一个Java程序,用于提示用户猜测计算机随机选择的数字。

huangapple go评论93阅读模式
英文:

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(&quot;Guess a number between 1 and 1000.&quot;);
num = input.nextInt();
if (num &gt;= 1 &amp;&amp; num &lt;= 1000)
{
while (checkNumber(num) != true)
{
System.out.println(&quot;Guess again&quot;);
num = input.nextInt();
checkNumber(num);
}
System.out.println(&quot;Congratulations. You &quot; +
&quot;guessed the number!&quot;);
}
}
public static int guess() {
return ( (int) (1 + Math.random()*1000) );
}
public static boolean checkNumber(int a){
int ans = guess();
if (a &lt; ans)
{
System.out.println(&quot;low&quot;);
return false;
}
else if (a &gt; ans)
{
System.out.println(&quot;high&quot;);
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(&quot;Guess a number between 1 and 1000: &quot;);
// Get user guess
int guess = input.nextInt();
while (guess != randomNumber) {
// Informs the user whether his guess is high or low
if (guess &gt; randomNumber) {
System.out.println(&quot;Your guess is high&quot;);
} else {
System.out.println(&quot;Your guess is low&quot;);
}
// Asks the user for another guess
System.out.print(&quot;Make another guess : &quot;);
guess = input.nextInt();
}
System.out.println(&quot;Congratulations ! You guessed the number&quot;);
}
}

huangapple
  • 本文由 发表于 2020年7月29日 02:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63140266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定