2

構文エラーが発生しましたが、その理由がわかりません。私はプログラミングコースの入門コースにいるので、私の知識は非常に限られています。これは私がしなければならないプロジェクトであり、これまでのコードです。正しい道を進んでいるかどうかをテストしたいだけですが、できません。どんな助けでも大歓迎ですので、少なくとも私の次の問題がどうなるかを見ることができます。

import java.util.*;

public class project2 {

    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        System.out.println("Welcome, I am thinking of a number between 1-100. Take a guess!");
        System.out.print("Please enter your number: ");
        int guess = in.nextInt();
        Random r = new Random(101);
        int counter = 0;
        int totalcount = 6;
        While (counter != totalcount) {      // being told to insert ";" and I have no clue why
            if ( guess < r) {
                System.out.print("You guessed " + guess);
                System.out.println("Too Low");
            } else if ( guess > r) {
                System.out.print("You guessed " + guess);
                System.out.println("Too High");
            } else ( guess = r ) {
                System.out.print("You guessed " + guess);
                System.out.println("YOU'RE PSYCHIC!");
            }
            counter++;
        }
    }
}
4

7 に答える 7

6

You have incorrectly capitalised While. It should be while (all lower-case).

See the documentation for while.

However, there are a number of other issues with your code.

Firstly, if you have tested that a number is not greater than or less than it must, by definition be equal. Therefore, you can change

else ( guess = r ) { 

to

else

This is fortunate for you as guess = r will not test for equality. Instead it will assign the value of r to guess. Also, because else (condition) is a syntax error.

Secondly You are trying to compare an int to a Random in this line:

if ( guess < r) // r is of type Random, guess is of type Int.

What you should actually do is use your Random to obtain an int, e.g.:

Random rnd = new Random();
int r = rnd.nextInt();

if (guess < r) // This is now valid.

Thirdly You have seeded your random number generator with 101. This means that it will always return the same sequence of numbers. This may be what you want but, if it is not, you can just use the paramaterless constructor to seed it with the current time in milliseconds

Random rnd = new Random();
于 2013-02-27T16:28:34.360 に答える
3
else ( guess = r ) {

Change condition into comment.

else { // ( guess == r )
于 2013-02-27T16:28:42.790 に答える
2

Do not capitalize the word "while". Case is very important in Java.

于 2013-02-27T16:28:34.960 に答える
1

問題集。以下の修正を参照してください。

  1. 「While」を大文字にしないでください
  2. else を実行して別の条件を確認したい場合は、'else if (...)' を実行します。
  3. int を 'Random' 型の変数と比較することはできません。代わりに Random.nextInt() を保存して比較します
  4. 比較するとき、「等しい」が必要な場合は、「=」ではなく「==」を使用してください。後者は代入演算子で、'==' は比較演算子です。

     import java.util.Random;
    
     import java.util.Scanner;
    
     public class project2 {
     public static void main(final String[] args) {
    
        Scanner in = new Scanner(System.in);
    
        System.out.println("Welcome, I am thinking of a number between 1-100. Take a guess!");
    
        System.out.print("Please enter your number: ");
    
        int guess = in.nextInt();
    
        Random r = new Random(101);
        int actual = r.nextInt();
    
        int counter = 0;
    
        int totalcount = 6;
    
        while (counter != totalcount) { // being told to insert ";" and I have no clue why
    
            if (guess < actual) {
    
                System.out.print("You guessed " + guess);
    
                System.out.println("Too Low");
            }
    
            else if (guess > actual) {
    
                System.out.print("You guessed " + guess);
    
                System.out.println("Too High");
            }
    
            else if (guess == actual) {
    
                System.out.print("You guessed " + guess);
    
                System.out.println("YOU'RE PSYCHIC!");
    
            }
    
            counter++;
        }
    
    }
    

    }

于 2013-02-27T16:32:16.727 に答える
0

guess = r比較ではなく代入です。この場合、デフォルトのケースとして比較は不要ですが、将来の参考guess == rのために、比較を行う正しい方法です (推測が int または他のプリミティブであると仮定します)。

于 2013-02-27T16:31:28.633 に答える
0

変化する

Random r = new Random(101);

Random random = new Random();
int r = random.nextInt(100) + 1;

int推測と比較するにはが必要です。Random乱数発生器である a を使用しています。このRandomメソッドnextInt(int i)は、0 (0 を含む) からそれ以外の範囲の数値を返しますi

于 2013-02-27T16:35:11.730 に答える
0

3つのものを見つけました

1) 大文字が間違っている間

2) r は整数と比較するには間違った型です。「ランダム」のインスタンスです。

3) あなたは == を意味し、= ではありません

プログラムをEclipseにロードすることで、これらすべてのエラーを見つけました。これらの問題を解決するために、同様のエディターを入手することをお勧めします!

お役に立てれば

于 2013-02-27T16:36:40.760 に答える