简体   繁体   English

我的代码无法运行,但没有错误

[英]My code won't run, but has no errors

I have two questions:我有两个问题:

  1. Why won't my program run?为什么我的程序不能运行?
  2. How can i make it check if the input isn't equal to some something?我怎样才能检查输入是否不等于某些东西?

-- ——

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);
    }
}

It will run and wait for user input at它将运行并等待用户输入

static String input = sc.nextLine();

I think you are not able to observe the console while you run it in eclipse.我认为您在 eclipse 中运行控制台时无法观察到它。 See if the java process is present in the process list查看进程列表中是否存在java进程

It runs fine for me, but it waits for user input before it outputs any text.它对我来说运行良好,但它在输出任何文本之前等待用户输入。 You might want to move your two static lines into the main method after the "Do you think..." line:您可能希望在“您认为...”行之后将两个静态行移动到 main 方法中:

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);
    }
}

static initializers are executed before the main method.静态初始化器在 main 方法之前执行。 So, it's waiting for input before anything is being output, giving the impression that nothing is happening.因此,它在输出任何内容之前等待输入,给人的印象是什么都没有发生。

To check if a string value is not equal, use the not operator "!"要检查字符串值是否不相等,请使用非运算符“!” as shown here:如图所示:

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

For numeric values, just use != as shown here:对于数值,只需使用 != ,如下所示:

if ( x != 3 )

Your program does"RUN" it is just you don't have any prmpots==>> System.out.println("Please tyoe your input") /// Or any other sort of prompt messages to notify the user.您的程序确实“运行”,只是您没有任何 prmpots==>> System.out.println("请输入您的输入") /// 或任何其他类型的提示消息来通知用户。 As for your Scanner , I removed the invalid modifier "Static" and moved to inside your MAIN class.As for the logic I am not sure what its supposed to be doing.至于您的 Scanner ,我删除了无效修饰符“Static”并移至您的 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);




      }
    }

These two lines:这两行:

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

Do the following:请执行下列操作:

  • Construct a new Scanner that uses the standard input构造一个使用标准输入的新Scanner
  • Read the first line from the ScannerScanner读取第一行

Since this is not declared inside a method, this happens pretty much right when you run the program (I won't confuse you with the technicalities).由于这不是在方法中声明的,因此当您运行程序时,这几乎是正确的(我不会将您与技术性混淆)。

So it's waiting to read the input before it outputs anything.所以它在输出任何东西之前等待读取输入。

So you perhaps want to put those two statements, with static removed, inside the main method, perhaps after this line:所以你可能想把这两个语句,去掉static ,放在main方法中,也许在这一行之后:

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

If this is not the problem, you're indeed probably not running it at all, and you'll have to provide more details as to how you're trying to run it.如果这不是问题,则您确实可能根本没有运行它,并且您必须提供有关如何尝试运行它的更多详细信息。

To check if your input isn't equal to something, you can either just say !input.equals("something") (inside an if-statement, the ! here means "not"), or you can use else :要检查您的输入是否不等于某物,您可以只说!input.equals("something") (在 if 语句中,这里的!表示“不”),或者您可以使用else

if (input.equals("something"))
   System.out.println("It's equal");
else
   System.out.println("It's not equal");

您能否检查一下您是否将文件命名为 HeigherOrLower.java,否则对其进行适当的重命名,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM