Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试无效时无法输出的值 import java.util.Scanner; 公共类倍数{ 公共静态void main(字符串[]args) { 整数; 扫描仪kb=新扫描仪(System.in); 系统输出打印(“插入数字>”; number=kb.nextInt(); 如果(number>=0&&number_Java - Fatal编程技术网

测试无效时无法输出的值 import java.util.Scanner; 公共类倍数{ 公共静态void main(字符串[]args) { 整数; 扫描仪kb=新扫描仪(System.in); 系统输出打印(“插入数字>”; number=kb.nextInt(); 如果(number>=0&&number

测试无效时无法输出的值 import java.util.Scanner; 公共类倍数{ 公共静态void main(字符串[]args) { 整数; 扫描仪kb=新扫描仪(System.in); 系统输出打印(“插入数字>”; number=kb.nextInt(); 如果(number>=0&&number,java,Java,您将希望在最顶端的if import java.util.Scanner; public class Multiples { public static void main(String[] args) { int number; Scanner kb = new Scanner(System.in); System.out.print("insert a number >"); number = kb.nextInt(); if (

您将希望在最顶端的
if

import java.util.Scanner;

public class Multiples {

  public static void main(String[] args) 
  {
    int number;
    Scanner kb = new Scanner(System.in);
    System.out.print("insert a number >");
    number = kb.nextInt();

    if (number >= 0 && number <= 100)
      if (number % 7 == 0)
        System.out.println("multiple of 7");
      else 
        System.out.println("not a multiple of 7");
      if (number % 2 == 0)
        System.out.println(" multiple of 2");
      else
        System.out.println("not a multiple of 2");
    else
      System.out.println("choose a different number");
  }

}
最后一个
else
没有对应的
if
,这在Java中是语法错误的


我们都建议您始终使用括号,无论您希望在
if-else
块中执行多少条语句。

使用一些括号…您希望在顶部
if
中有两条语句。从文档:“决定何时省略大括号是个人喜好的问题。省略大括号会使代码更加脆弱。如果在“then”子句中添加第二条语句,常见的错误是忘记添加新需要的大括号。编译器无法捕获此类错误;您只会得到错误的结果。”。“即使是代码格式化程序也会向您显示此错误,但这就是为什么您应该始终使用括号的一个示例。诚挚的歉意现在我理解了此错误。最好在所有
if
上使用括号。
if (number >= 0 && number <= 100) {
    if (number % 7 == 0)
        System.out.println("multiple of 7");
    else 
        System.out.println("not a multiple of 7");
    if (number % 2 == 0)
        System.out.println(" multiple of 2");
    else
        System.out.println("not a multiple of 2");
} else
    System.out.println("choose a different number");
if (number >= 0 && number <= 100) {
    if (number % 7 == 0)
        System.out.println("multiple of 7");
    else 
        System.out.println("not a multiple of 7");
}
if (number % 2 == 0)
    System.out.println(" multiple of 2");
else
    System.out.println("not a multiple of 2");

else
    System.out.println("choose a different number");