英文:
A java program to prompt the user to guess a number chosen by the computer at random
问题
我是Java的初学者,我试图制作一个程序,用来在1到1000之间选择一个随机数,并提示用户猜测这个数字,但是程序似乎不起作用,我不知道哪里出了问题。
package com.company;
import java.util.Scanner;
public class GradeBookTest {
public static void main(String[] args) {
int num;
Scanner input = new Scanner(System.in);
guess();
System.out.println("猜一个介于1和1000之间的数字。");
num = input.nextInt();
if (num >= 1 && num <= 1000)
{
while (checkNumber(num) != true)
{
System.out.println("再猜一次");
num = input.nextInt();
checkNumber(num);
}
System.out.println("恭喜你。你猜对了数字!");
}
}
public static int guess() {
return ( (int) (1 + Math.random()*1000) );
}
public static boolean checkNumber(int a){
int ans = guess();
if (a < ans)
{
System.out.println("低");
return false;
}
else if (a > ans)
{
System.out.println("高");
return false;
}
else
return true;
}
}
英文:
I am a beginner in Java and I was trying to make this program to choose a random number between 1 and 1000 and prompt the user to guess that number but the program doesn't seem to work and I don't know what's wrong with it.
package com.company;
import java.util.Scanner;
public class GradeBookTest {
public static void main(String[] args) {
int num;
Scanner input = new Scanner(System.in);
guess();
System.out.println("Guess a number between 1 and 1000.");
num = input.nextInt();
if (num >= 1 && num <= 1000)
{
while (checkNumber(num) != true)
{
System.out.println("Guess again");
num = input.nextInt();
checkNumber(num);
}
System.out.println("Congratulations. You " +
"guessed the number!");
}
}
public static int guess() {
return ( (int) (1 + Math.random()*1000) );
}
public static boolean checkNumber(int a){
int ans = guess();
if (a < ans)
{
System.out.println("low");
return false;
}
else if (a > ans)
{
System.out.println("high");
return false;
}
else
return true;
}
}
答案1
得分: 1
以下是代码的翻译部分:
public class Guess {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// 生成随机数
int randomNumber = (int)(Math.random() * 1000);
System.out.print("猜一个介于1和1000之间的数字:");
// 获取用户猜测
int guess = input.nextInt();
while (guess != randomNumber) {
// 告诉用户他的猜测是高还是低
if (guess > randomNumber) {
System.out.println("你的猜测太高了");
} else {
System.out.println("你的猜测太低了");
}
// 要求用户再次猜测
System.out.print("再猜一次:");
guess = input.nextInt();
}
System.out.println("恭喜!你猜对了数字");
}
}
英文:
Try this
public class Guess {
public static void main (String [] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Generates the Random Number
int randomNumber = (int)(Math.random() * 1000);
System.out.print("Guess a number between 1 and 1000: ");
// Get user guess
int guess = input.nextInt();
while (guess != randomNumber) {
// Informs the user whether his guess is high or low
if (guess > randomNumber) {
System.out.println("Your guess is high");
} else {
System.out.println("Your guess is low");
}
// Asks the user for another guess
System.out.print("Make another guess : ");
guess = input.nextInt();
}
System.out.println("Congratulations ! You guessed the number");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论