我的代码不会运行,但没有错误

时间:2014-01-21 14:43:30

标签: java input equals

我有两个问题:

  1. 为什么我的程序不会运行?
  2. 如何检查输入是否与某些输入不相符 什么?
  3. -

    import java.util.Random;
    import java.util.Scanner;
    
    public class HeigherOrLower {
    
            //Creates the scanner.
        static Scanner sc = new Scanner(System.in);
        static String input = sc.nextLine();
    
        public static void main(String[] args)
        {
            //Creating the two random numbers.
            Random rand = new Random();
            int userNumber  = rand.nextInt(10) + 1;
            int comNumber = rand.nextInt(10) + 1;
    
            System.out.println("Your number is: " + userNumber +" of 10");
            System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");
    
                //Checking if the user is right.
            if(userNumber < comNumber && input.equals("L"))
                System.out.println("You are right. The computer's number is: " + comNumber);
    
            if(userNumber == comNumber && input.equals("E"))
                System.out.println("You are right. The computer's number is: " + comNumber);
    
            if(userNumber > comNumber && input.equals("H"))
                    System.out.println("You are right. The computer's number is: " + comNumber);
        }
    }
    

5 个答案:

答案 0 :(得分:0)

它将在

运行并等待用户输入
static String input = sc.nextLine();

我认为你在eclipse中运行它时无法观察控制台。查看进程列表中是否存在java进程

答案 1 :(得分:0)

它对我运行正常,但它在输出任何文本之前等待用户输入。您可能希望在“您认为......”行之后将两条静态行移动到主方法中:

import java.util.Random;
import java.util.Scanner;


public class HeigherOrLower
{

    public static void main(String[] args)
    {
        //Creating the two random numbers.
        Random rand = new Random();
        int userNumber  = rand.nextInt(10) + 1;
        int comNumber = rand.nextInt(10) + 1;

        System.out.println("Your number is: " + userNumber +" of 10");
        System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");

        //Creates the scanner.
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        //Checking if the user is right.
        if(userNumber < comNumber && input.equals("L"))
            System.out.println("You are right. The computer's number is: " + comNumber);

        if(userNumber == comNumber && input.equals("E"))
            System.out.println("You are right. The computer's number is: " + comNumber);

        if(userNumber > comNumber && input.equals("H"))
            System.out.println("You are right. The computer's number is: " + comNumber);
    }
}

静态初始值设定项在main方法之前执行。因此,它在输出任何内容之前等待输入,给人的印象是没有任何事情发生。

要检查字符串值是否不相等,请使用not运算符“!”如下所示:

if ( ! input.equals("H") ) 

对于数值,只需使用!=,如下所示:

if ( x != 3 )

答案 2 :(得分:0)

你的程序确实“运行”它只是你没有任何prmpots ==&gt;&gt; System.out.println(“请输入您的输入”)///或任何其他类型的提示消息以通知用户。至于你的扫描仪,我删除了无效修饰符“静态”并移动到你的MAIN类中。至于逻辑,我不确定它应该做什么。

     public class HeigherOrLower {

    //Creates the scanner.


    public static void main(String[] args)
     {

        System.out.println("Please type your input");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
    //Creating the two random numbers.
    Random rand = new Random();
    int userNumber  = rand.nextInt(10) + 1;
    int comNumber = rand.nextInt(10) + 1;

    System.out.println("Your number is: " + userNumber +" of 10");
    System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");

        //Checking if the user is right.
    if(userNumber < comNumber && input.equals("L"))
        System.out.println("You are right. The computer's number is: " + comNumber);

    if(userNumber == comNumber && input.equals("E"))
        System.out.println("You are right. The computer's number is: " + comNumber);

    if(userNumber > comNumber && input.equals("H"))
            System.out.println("You are right. The computer's number is: " + comNumber);




      }
    }

答案 3 :(得分:0)

这两行:

static Scanner sc = new Scanner(System.in);
static String input = sc.nextLine();

执行以下操作:

  • 构建一个使用标准输入的新Scanner
  • 阅读Scanner
  • 的第一行

由于这不是在方法中声明的,因此在运行程序时会发生这种情况(我不会因为技术问题而混淆你)。

所以它在输出任何内容之前等待读取输入。

因此,您可能希望将static方法中的这两个语句放在main方法中,也许在此行之后:

System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number");

如果这不是问题,那么你可能根本就没有运行它,而且你必须提供更多关于你如何运行它的详细信息。

要检查您的输入是否与某些内容不相等,您可以只说!input.equals("something")(在if语句中,!在这里意味着“不是”),或者您可以使用{ {1}}:

else

答案 4 :(得分:-1)

您能否检查一下您的文件是否已命名为HeigherOrLower.java,否则请将其重命名,谢谢!