Java:代码ain';我的工作方式不对
首先你必须知道我是Java的新手,我刚刚开始编写代码,这是我的第一种编程语言。所以请不要生气,如果我开始有点愚蠢,我只是想学习编码-谢谢 我试图制作一个简单的“猜谜游戏”,但我的代码并没有等待用户输入。 请帮帮我,我不知道该怎么办 我的代码:Java:代码ain';我的工作方式不对,java,warnings,Java,Warnings,首先你必须知道我是Java的新手,我刚刚开始编写代码,这是我的第一种编程语言。所以请不要生气,如果我开始有点愚蠢,我只是想学习编码-谢谢 我试图制作一个简单的“猜谜游戏”,但我的代码并没有等待用户输入。 请帮帮我,我不知道该怎么办 我的代码: public static void main(String[] args) { //Creating the scanner @SuppressWarnings("resource") Sc
public static void main(String[] args)
{
//Creating the scanner
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
//Creating the two random numbers.
Random rand = new Random();
int userNumber = rand.nextInt(10) + 1;
int comNumber = rand.nextInt(10) + 1;
//Asks the user what to do.
System.out.println("Your number is: " + userNumber +" of 10");
System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");
//Checking if the user is right.
//If the user types in LOWER
if(userNumber < comNumber && input.equals("L"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber < comNumber && !input.equals("L"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
//If the user types in EQUAL TO.
if(userNumber == comNumber && input.equals("E"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber == comNumber && !input.equals("E"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
//If the user types in HEIGHER.
if(userNumber > comNumber && input.equals("H"))
System.out.println("You are right. The computer's number is: " + comNumber);
if(userNumber > comNumber && !input.equals("H"))
System.out.println("You are wrong. The computer's number is: " + comNumber);
else
System.out.println("You can only type in ' L ', ' E ' or ' H '.");
}
}
publicstaticvoidmain(字符串[]args)
{
//创建扫描仪
@抑制警告(“资源”)
扫描仪输入=新扫描仪(System.in);
//创建两个随机数。
Random rand=新的Random();
int userNumber=rand.nextInt(10)+1;
int comNumber=rand.nextInt(10)+1;
//询问用户要做什么。
System.out.println(“您的号码是:“+userNumber+”共10个”);
你认为你的数字是高(H)、低(L)还是等于(E)计算机数字;
//检查用户是否正确。
//如果用户键入较低的
if(userNumbercomname&&input.equals(“H”))
System.out.println(“你是对的,计算机的号码是:“+comNumber”);
如果(userNumber>comNumber&&!input.equals(“H”))
System.out.println(“你错了,计算机的号码是:“+comNumber”);
其他的
System.out.println(“您只能输入'L'、'E'或'H'”;
}
}
如果您能帮我解决我的问题,并告诉我如何删除@SuppressWarnings(“资源”)/请解释为什么它必须在那里。您使用的
扫描仪错误。您需要调用以从用户处获取输入(字符串),并且需要将字符串转换为整数(带),以将其与其他整数进行比较。您使用的扫描仪是错误的。您需要调用以从用户处获取输入(字符串),并且需要将字符串转换为整数(带),以将其与其他整数进行比较。您使用的扫描仪是错误的。您需要调用以从用户处获取输入(字符串),并且需要将字符串转换为整数(带),以将其与其他整数进行比较。您使用的扫描仪是错误的。您需要调用以从用户处获取输入(字符串),并且需要将字符串转换为整数(带),以将其与其他int进行比较。您必须将java.util.Scanner导入到项目中。看看这个链接,它会给你提供大量的信息
编辑:刚刚注意到你把它放在顶部。接下来要做的是创建一个变量,比如字符串类型的“guess”,然后将input.next()赋值给该变量。然后可以用guess.equals()函数替换input.equals()函数
您必须将java.util.Scanner导入到项目中。看看这个链接,它会给你提供大量的信息
编辑:刚刚注意到你把它放在顶部。接下来要做的是创建一个变量,比如字符串类型的“guess”,然后将input.next()赋值给该变量。然后可以用guess.equals()函数替换input.equals()函数
您必须将java.util.Scanner导入到项目中。看看这个链接,它会给你提供大量的信息
编辑:刚刚注意到你把它放在顶部。接下来要做的是创建一个变量,比如字符串类型的“guess”,然后将input.next()赋值给该变量。然后可以用guess.equals()函数替换input.equals()函数
您必须将java.util.Scanner导入到项目中。看看这个链接,它会给你提供大量的信息
编辑:刚刚注意到你把它放在顶部。接下来要做的是创建一个变量,比如字符串类型的“guess”,然后将input.next()赋值给该变量。然后可以用guess.equals()函数替换input.equals()函数
您应该导入java.util.*;
如果您不接受用户的任何输入,请尝试
int inp=input.nextInt()
您应该导入java.util.*;
如果您不接受用户的任何输入,请尝试
int inp=input.nextInt()
您应该导入java.util.*;
如果您不接受用户的任何输入,请尝试
int inp=input.nextInt()
您应该导入java.util.*;
如果您不接受用户的任何输入,请尝试
int inp=input.nextInt()
你可能想读书。它有一些关于如何正确使用它的例子
主要问题在于if语句:使用input.equals(“L”)
时,您会询问scanner对象是否等于字符串“L”,这是不可能的,因为它们的类型不同
要从输入流中获取字符串,可以使用input.next()
并将其与“L”进行比较。记住在所有ifs之前只调用一次,否则程序在每次条件检查时都会等待新的输入
关于警告:
正如您在文档中所看到的,扫描仪在使用后需要使用input.close()
关闭
作为提示,永远不要使用@SuppressWarnings注释
String expectedInput;
if (userNumber < comNumber) {
expectedInput = "L";
} else if (userNumber == comNumber) {
expectedInput = "E";
} else {
expectedInput = "H";
}
String userInput = input.next();
if (userInput.equals(expectedInput)) {
System.out.println("You are right. The computer's number is: " + comNumber);
} else {
System.out.println("You are wrong. The computer's number is: " + comNumber);
}
String userInput = input.next();
boolean isInputValid = Arrays.asList("L", "E", "H").contains(userInput);
if (isInputValid) {
String rightWrong = userInput.equals(expectedInput) ? "right" : "wrong";
System.out.println("You are " + rightWrong + ". The computer's number is: " + comNumber);
} else {
System.out.println("You can only type in ' L ', ' E ' or ' H '.");
}
if(guess.equals("L")){
if(userNumber < comNumber){
System.out.println("You are right. The computer's number is: " + comNumber);
} else {
System.out.println("You are wrong. The computer's number is: " + comNumber);
}
} else if(guess.equals("E")){
if(userNumber == comNumber){
System.out.println("You are right. The computer's number is: " + comNumber);
} else {
System.out.println("You are wrong. The computer's number is: " + comNumber);
}
} else if(guess.equals("H")){
if(userNumber > comNumber){
System.out.println("You are right. The computer's number is: " + comNumber);
} else {
System.out.println("You are wrong. The computer's number is: " + comNumber);
}
} else {
System.out.println("You can only type in ' L ', ' E ' or ' H '.");
}