无法使用System.out.println();

时间:2017-08-12 20:43:57

标签: java

请帮帮我。 Eclipse说在if子句中是个错误。我不允许在那里使用Println if(eingabe = z1) 也是一个错误(eingabe = z1)是红色

import java.util.*;
import java.util.Scanner;

public class Benedikt {

    public static void main(String[] args){
        Random zufall = new Random ();
        int eingabe;    
        Scanner leser1= new Scanner (System.in);

        int z1;
        z1 = zufall.nextInt(6)+1;

        System.out.println("I have got a number between 1 and 6 in my mind. %n");
        System.out.println("Try to guess it!");

        //Eingabe
        eingabe=leser1.nextInt(eingabe);

        if (eingabe=z1){
            System.out.println("You have found out the number! My number was %d",z1);
        } else if(eingabe > z1){
            System.out.println("My number is smaller than %d", eingabe);
        } else if (eingabe<z1){
            System.out.println("My number is smaller than %d", eingabe);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

当您需要比较(if)相等时,您必须使用==而非=(这是为变量赋值)。

prinln您需要使用printf

System.out.printf("You have found out the number! My number was %d\n", z1);

println()不允许您在其括号内提供格式说明符。它与print()类似,只是println()在打印后添加下一行。

printf()允许您编写%d%s等格式说明符,因此您可以在代码中的任何位置使用printf() print() println() 1}}或\n(使用 var data = {'name': 'john', 'old': 18, 'place': 'USA'} 获取新行。)

答案 1 :(得分:1)

  1. println只接受一个参数。您可以使用printf(但应该为换行符添加%n):

    System.out.printf("You have found out the number! My number was %d%n", z1);
    

    或连接字符串:

    System.out.println("You have found out the number! My number was " + z1);
    
  2. if (eingabe = z1)应为if (eingabe == z1)eingabe = z1是一个赋值,不返回布尔值。