java手动输入函数_Java程序如何添加一个函数,如果玩家输入错误,它将返回代码。...

我的程序有问题,不是代码问题,而是我要怎么做,这是我一直困惑的部分。只是为了让你知道我是一个基本的Java编码器,我不理解复杂的东西,所以请记住,我的代码不是最好的。

——————————————————————————————————————————————程序说明---

在我向您展示我的问题之前,让我们先来解释一下它是如何工作的,好吧,当您执行程序时,它会提示您一个类似于视频游戏中的菜单,但它是基于文本的,它会向您显示不同的选项,如输入玩家详细信息、玩数学游戏显示分数,然后退出。输入玩家详细信息它告诉玩家1输入他/她姓名,然后告诉另一个输入他/她玩家姓名,然后提示您返回菜单。玩数学游戏是要求玩家1输入他/她数学公式,然后玩家2必须解决它,如果他得到正确的他/她得到10分,如果没有,玩家没有得到任何点。然后重复让另一个玩家输入他/她数学公式,然后提示您返回菜单。显示分数-显示谁在数学游戏中得到的分数最多-计算谁得到的分数最多如果他们两个得到的分数相同,则表示平局,然后提示您返回菜单。最后一件事,退出选项,当你选择该选项,它停止程序。如果玩家选择了一个错误的选项,他会收到一条错误信息,并将您放回菜单

好的,这里是第一个类,叫做menu,另一个类,它与名为game parties的菜单相连接

所以这段代码我自己找不到

import java.util.*;

public class Gamefunctions // this is a core when player choosess one of these options from the menu

{

String[] player =new String[2];

double scorea = 0; // verribles of all the objects

double scoreb = 0;

int i;

Scanner input = new Scanner(System.in);

double answer = 0;

double numA, numB;

char operator;

char operator2;

boolean quit = false;

double sum1;

double sum2;

public void enterDetails(){ // if player select enter details

for ( i=0;i<2;i++) {// tell's player to input he/she's name and stores them

int c=i;

System.out.println("Welcome to the maths quiz game please input player name "+c++);

player[i] = input.next();

}

}

public void mathGame(){ // if player select enter details

System.out.println("Please enter your equation please "+player[0]+" press enter for each number and mathematical symbol"); // tells the player 1 to input

System.out.println("");

System.out.println("such as for ex input a number or how many you like, then hit enter and input such as /*-+^ hit enter, then input any number one or how many you like ");

String s=input.next();

numA = Double.parseDouble(s); // numa and numb and operator is the aera of player to input he/she equation

operator = input.next().charAt(0);

numB = input.nextDouble();

if () {

if (operator == '+') {// this is if operator is one of these like +-*/^ and then it works out the sum

answer = numA + numB;

}

if (operator == '-') {

answer = numA - numB;

}

if (operator == '*') {

answer = numA * numB;

}

if (operator == '/') {

answer = numA / numB;

}

if (operator == '^') {

answer = Math.pow(numA, numB);

}

} else {

System.out.println("error input like for an example '10' enter '+' enter '10'");

}

System.out.println("");

System.out.println(player[1]+"\t solve the equation"); // tells other player to slove the equation

sum2 = input.nextDouble();

if (sum2 == answer){// checks if the answer from the player is good or not if its good he/she gets 10 points if he/she gets it wrong gets no points and shows the right answer so the player learns from his/she mistakes

scoreb = scoreb + 10.00;

System.out.println("correct you got 10 points to your score");

System.out.println("");

} else{

System.out.println("incorrect you got no points the correct answer was:"+"" + answer);

}

你知道当程序要求玩家输入他的数学方程并输出它,然后继续程序并等待用户输入

public void mathGame(){ // if player select enter details

System.out.println("Please enter your equation please "+player[0]+" press enter for each number and mathematical symbol"); // tells the player 1 to input

System.out.println("");

System.out.println("such as for ex input a number or how many you like, then hit enter and input such as /*-+^ hit enter, then input any number one or how many you like ");

String s=input.next();

numA = Double.parseDouble(s); // numa and numb and operator is the aera of player to input he/she equation

operator = input.next().charAt(0);

numB = input.nextDouble();

假设玩家输入的是10+10回车,但由于它们存储在一个int的numa中,所以不能工作,我想发一条错误消息说你不能输入10+10,你必须输入10+10回车,这样它才能工作

如果播放机输入正确,它将继续程序

所以,如果你解释我的文章有任何问题,请允许我编辑它,谢谢你的时间:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值