2017-08-12 29 views
-4

请帮帮我。 Eclipse表示在if子句中是一个错误。我不允许使用的println那里 如果(eingabe = Z1) 也是一大错误(eingabe = Z1)是红色无法使用System.out.println();

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

'eingabe = z1'是赋值。 –

+0

@GhostCat:有'main' – Blasanka

回答

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是一个赋值,不返回布尔值。

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和等,因此,你可以在这里你先前已使用print()println()(与\n获得新的生产线)使用printf()在你的代码的任何地方。

+0

@GhostCat:有main(),我在我的解决方案中看不到任何问题 – Blasanka

+0

为什么有人低估了我的答案。 – Blasanka

+0

@DawoodibnKareem感谢您的好意!我已经更新了我的答案。 – Blasanka

相关问题