好吧所以无论我输入的第二个猜测是什么,它说它高于60但不是,这可能是一个菜鸟错误,但我不明白为什么?如果有人可以运行此修复错误并解释什么是错误的,这将是一个很大的帮助
package testing;
import java.util.Scanner;
import java.util.Random;
public class testerclass {
public static void main (String args[]){
double user1ans, user2ans, total, number, guess1, guess2, count, diff1, diff2;
Random dice = new Random();
Scanner user1 = new Scanner(System.in);
Scanner user2 = new Scanner(System.in);
System.out.println("Enter player one's guess: ");
guess1 = user1.nextInt();
System.out.println("Enter player two's guess: ");
guess2 = user2.nextInt();
if (guess1 > 60 || guess2 > 60);
System.out.println("You can't guess higher than 60!");
testerclass.main(args);
for (count=1; count<=10; count++){
number = dice.nextInt(6+1);
total = number + number;
System.out.println(number);
System.out.println(total);
user1ans = guess1 - total;
user2ans = guess2 - total;
if (user1ans == 0 && user2ans == 0);
System.out.println("Its a draw!");
testerclass.main(args);
if (user1ans == 0);
System.out.println("Player one wins!!");
testerclass.main(args);
if (user2ans == 0);
System.out.println("Player two wins!!");
testerclass.main(args);
if (user1ans == user2ans && user2ans == user1ans);
System.out.println("Its a draw!!");
testerclass.main(args);
diff1 = Math.abs(guess1 - total);
diff2 = Math.abs(guess2 - total);
if (diff1 < diff2);
System.out.println("Player one wins");
testerclass.main(args);
if (diff2 < diff1);
System.out.println("Player two wins");
testerclass.main(args);
}
}
}
答案 0 :(得分:0)
(1)不要使用2个扫描仪,一个就够了:)(当你只从一个来源读取输入时 - 一个命令行)
(2)在你的第一个for循环中,第二行应该是&#34; total = total + number&#34; (?)
(3)此外,您需要一个区别,所以在guess1 - total
中包裹guess2 - total
和Math.abs()
:
user1ans = Math.abs(total - guess1);
user2ans = Math.abs(total - guess2)
(4)if
内的所有命令/功能都应该在一个正文中(例如&#34;好&#34; if):
if(STATEMENT)
{
// Things to do go here
}
因为否则(就像在你的代码中一样)它将执行代码,无论if语句是真还是假(因为;
之后有if
(5)我还建议在新功能中复制整个代码,然后拨打return;
而不是testerclass.main(args)
。
(我知道,我不应该这样做,但在这里,最后的代码)
import java.util.Random;
import java.util.Scanner;
/**
* Created by cyphrags on 28.04.15.
*/
public class test
{
public static void main (String args[]){
while(true)
{
GuessIt();
}
}
public static void GuessIt()
{
double user1ans, user2ans, total = 0, number, guess1, guess2, count, diff1, diff2;
Random dice = new Random();
Scanner in = new Scanner(System.in);
System.out.println("Enter player one's guess: ");
guess1 = in.nextInt();
System.out.println("Enter player two's guess: ");
guess2 = in.nextInt();
if (guess1 > 60 || guess2 > 60) {
System.out.println("You can't guess higher than 60!");
return;
}
for (count=1; count<=10; count++) {
number = dice.nextInt(6 + 1);
total = number + number;
//System.out.println(number);
//System.out.println(total);
}
System.out.println(total);
user1ans = Math.abs(guess1 - total);
user2ans = Math.abs(guess2 - total);
if (user1ans == user2ans) {
System.out.println("Its a draw!");
} else if (user1ans < user2ans) {
System.out.println("Player one wins!!");
} else {
System.out.println("Player two wins!!");
}
}
}