为什么java输出我的最后一个方法

时间:2015-03-30 16:08:37

标签: java loops nested nested-loops

文件:/// C:/用户/克里斯/下载/ lab04_guessinggame%20(6).PDF    *代码不会输出最后一个方法

import java.util.*;

public class GuessingGame{
    public static void main( String[] args ){
        int x=getRandomNumber();
        int y=getGuessFromUser(x);
        int v=getGuessFromUser(x);
    }

    public static int getRandomNumber(){
        return (int)(Math.random()*26);
    }

    public static int getGuessFromUser(int x){
        Scanner keyboard = new Scanner(System.in);
        int y;
        int v=0;
        do{
            System.out.print("Enter a number between 1 and 25: ");
            y=keyboard.nextInt();
            v++;
        } while(y<=0||y>25 || v<5);
        return y;
    }

    public static void main(int x, int y, int v){
        do{
            if(x>y)
                System.out.println("TOO LOW");
            if(x<y)
                System.out.println("TOO HIGH");
            if(x==y)
                System.out.print("YOU ARE CORRECT! IT TOOK YOU " + v + " GUESSES!");
        }while(v<5);
    }
}
  

主要方法不会输出

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

public class GuessingGame{
    public static void main( String[] args ){
        int x=getRandomNumber();
        int y=getGuessFromUser(x);
        int v=getGuessFromUser(x);
        output(x,y,v);
    }

    public static int getRandomNumber(){
        return (int)(Math.random()*26);
    }

    public static int getGuessFromUser(int x){
        Scanner keyboard = new Scanner(System.in);
        int y;
        int v=0;
        do {
            System.out.print("Enter a number between 1 and 25: ");
            y=keyboard.nextInt();
            v++;
        } while(y<=0||y>25 || v<5);
        return y;
    }

    public static void output(int x, int y, int v){
        do {
            if (x>y)
                System.out.println("TOO LOW");
            if (x<y)
                System.out.println("TOO HIGH");
            if (x==y)
                System.out.print("YOU ARE CORRECT! IT TOOK YOU " + v + " GUESSES!");
        } while (v<5);
    }
}

正如人们在评论中所说,你不能有两种主要方法,所以你必须重命名最后一种,然后在某处调用它。话虽如此,我只是修改了你的代码,但你可能想要更多地思考它的结构。

答案 1 :(得分:0)

我不知道你想通过这个代码实现什么,但关于

  

主要方法不会输出

就我所见,您的代码中存在2个问题

  1. 你打电话给main(int,int,int)
  2. main(int,int,int)中,您有一个无限循环,因为V永远不会改变。
  3. 所以代码是

    import java.util.*;
    
    public class GuessingGame{
    public static void main( String[] args ){
    int x=getRandomNumber();
    int y=getGuessFromUser(x);
    int v=getGuessFromUser(x);
    main(x, y,v);
    }
    
    
    public static int getRandomNumber(){
    return (int)(Math.random()*26);
    }
    public static int getGuessFromUser(int x){
    Scanner keyboard = new Scanner(System.in);
    int y;
    int v=0;
    do{
    System.out.print("Enter a number between 1 and 25: ");
    y=keyboard.nextInt();
    v++;
    }while(y<=0||y>25 || v<5);
    return y;
    }
    public static void main(int x, int y, int v){
    do{
    if(x>y)
    System.out.println("TOO LOW");
    if(x<y)
    System.out.println("TOO HIGH");
    if(x==y)
    System.out.print("YOU ARE CORRECT! IT TOOK YOU " + v + " GUESSES!");
    }while(v++<5);
    }
    }
    

答案 2 :(得分:0)

这应该改变:

public static void main(int x, int y, int v){
do{
if(x>y)
System.out.println("TOO LOW");
if(x<y)
System.out.println("TOO HIGH");
if(x==y)
System.out.print("YOU ARE CORRECT! IT TOOK YOU " + v + " GUESSES!");
}while(v<5);
}

为:

public static void checkGuess(int randomNumber, int tries){
    do{
        int guess = getGuessFromUser(randomNumber);
        tries++;

        if(randomNumber > guess) System.out.println("TOO LOW");

        if(randomNumber < guess) System.out.println("TOO HIGH");

        if(randomNumber == guess) 
        System.out.print("YOU ARE CORRECT! IT TOOK YOU " + tries + " GUESSES!");
    }while(tries < 5);
}

对main的更改:

public static void main( String[] args ){
    int x=getRandomNumber();
    int v = 0;
    checkGuess(x, v);
}

对getGuessFromUser的更改:

public static int getGuessFromUser(int x){
    Scanner keyboard = new Scanner(System.in);
    int y;

    do{
        System.out.print("Enter a number between 1 and " + x + ": ");
        y=keyboard.nextInt();

    }while(y <= 0 || y > x);

    return y;
}

代码中存在许多逻辑和语法问题。我建议您在代码中一步一步地思考它的作用。另外,不要忘记仔细检查变量的范围。