java手动输入函数_Java程序如何添加一个函数,如果玩家输入错误,它将返回代码。...
我的程序有問題,不是代碼問題,而是我要怎么做,這是我一直困惑的部分。只是為了讓你知道我是一個(gè)基本的Java編碼器,我不理解復(fù)雜的東西,所以請記住,我的代碼不是最好的。
——————————————————————————————————————————————程序說明---
在我向您展示我的問題之前,讓我們先來解釋一下它是如何工作的,好吧,當(dāng)您執(zhí)行程序時(shí),它會提示您一個(gè)類似于視頻游戲中的菜單,但它是基于文本的,它會向您顯示不同的選項(xiàng),如輸入玩家詳細(xì)信息、玩數(shù)學(xué)游戲顯示分?jǐn)?shù),然后退出。輸入玩家詳細(xì)信息它告訴玩家1輸入他/她姓名,然后告訴另一個(gè)輸入他/她玩家姓名,然后提示您返回菜單。玩數(shù)學(xué)游戲是要求玩家1輸入他/她數(shù)學(xué)公式,然后玩家2必須解決它,如果他得到正確的他/她得到10分,如果沒有,玩家沒有得到任何點(diǎn)。然后重復(fù)讓另一個(gè)玩家輸入他/她數(shù)學(xué)公式,然后提示您返回菜單。顯示分?jǐn)?shù)-顯示誰在數(shù)學(xué)游戲中得到的分?jǐn)?shù)最多-計(jì)算誰得到的分?jǐn)?shù)最多如果他們兩個(gè)得到的分?jǐn)?shù)相同,則表示平局,然后提示您返回菜單。最后一件事,退出選項(xiàng),當(dāng)你選擇該選項(xiàng),它停止程序。如果玩家選擇了一個(gè)錯誤的選項(xiàng),他會收到一條錯誤信息,并將您放回菜單
好的,這里是第一個(gè)類,叫做menu,另一個(gè)類,它與名為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);
}
你知道當(dāng)程序要求玩家輸入他的數(shù)學(xué)方程并輸出它,然后繼續(xù)程序并等待用戶輸入
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();
假設(shè)玩家輸入的是10+10回車,但由于它們存儲在一個(gè)int的numa中,所以不能工作,我想發(fā)一條錯誤消息說你不能輸入10+10,你必須輸入10+10回車,這樣它才能工作
如果播放機(jī)輸入正確,它將繼續(xù)程序
所以,如果你解釋我的文章有任何問題,請?jiān)试S我編輯它,謝謝你的時(shí)間:
總結(jié)
以上是生活随笔為你收集整理的java手动输入函数_Java程序如何添加一个函数,如果玩家输入错误,它将返回代码。...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的ul是什么标签_li和ul标
- 下一篇: java request 原理_Java