qq_37290151
XIAO MING
2017-10-26 13:25

Java小程序的执行流程

  • java

import java.util.Random;
import java.util.Scanner;
/**

  • Starter code for the "Guess My Number" exercise
  • 为什么我觉得逻辑没有问题,但是没有完成想要的效果呢?
    */
    public class GuessStarter{

    public static void main(String[] args){
    Random random = new Random();
    int number;
    int inputNumber;
    Scanner in = new Scanner(System.in);

    number = random.nextInt(100) + 1;
    System.out.println("Type a number: ");
    inputNumber = in.nextInt();
    check(number, inputNumber);
    

    }
    public static void check(int number, int inputNumber){
    if(number > inputNumber){
    System.out.println("The number you guess is too big");
    System.out.println("Guess again: ");
    Scanner in1 = new Scanner(System.in);
    inputNumber = in1.nextInt();
    check(number, inputNumber);

    }else if(number < inputNumber){
        System.out.println("The number you guess is too small");
        System.out.println("Guess again: ");
        Scanner in2 = new Scanner(System.in);
        inputNumber = in2.nextInt();
        check(number, inputNumber);
    
    }else{
        System.out.println("Congratulations!You are right!");
    }
    

    }
    }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答