资讯详情

[eclipse]Scanner使用

写完以下代码后,出现了" java.util.NoSuchElementException"

中文的错误是:没有这样的元素异常

原因是sc.close();应该放在最后,不会报错

package ShuJuShuRu;

import java.util.Scanner;

public class shujushuru { public static void main(String[] args) { System.out.println("请输入正整数:"); //创建对象 Scanner sc=new Scanner(System.in); //接收数据 int x=sc.nextInt(); System.out.println(x); sc.close(); System.out.println("--例子三个和尚升级版--\n"); System.out.println("请输入第一个和尚的身高:"); Scanner hs1=new Scanner(System.in); int height1=hs1.nextInt(); System.out.println("请输入第二个和尚的身高:"); Scanner hs2=new Scanner(System.in); int height2=hs2.nextInt(); System.out.println("请输入第3个和尚的身高:"); Scanner hs3=new Scanner(System.in); int height3=hs3.nextInt(); int max1=height1>height2 ? height1:height2; int max=max1>height3 ? max1:height3; System.out.println("三位和尚中最高的身高是:" max); hs1.close(); hs2.close(); hs3.close(); } }

正确代码如下:

package ShuJuShuRu;

import java.util.Scanner;

public class shujushuru { public static void main(String[] args) { System.out.println("请输入正整数:"); //创建对象 Scanner sc=new Scanner(System.in); //接收数据 int x=sc.nextInt(); System.out.println(x); System.out.println("--例子三个和尚升级版--\n"); System.out.println("请输入第一个和尚的身高:"); Scanner hs1=new Scanner(System.in); int height1=hs1.nextInt(); System.out.println("请输入第二个和尚的身高:"); Scanner hs2=new Scanner(System.in); int height2=hs2.nextInt(); System.out.println("请输入第三个和尚的身高:"); Scanner hs3=new Scanner(System.in); int height3=hs3.nextInt(); int max1=height1>height2 ? height1:height2; int max=max1>height3 ? max1:height3; System.out.println("三位和尚中最高的身高是:" max); sc.close(); hs1.close(); hs2.close(); hs3.close(); } }

标签: hs3一组常开10a继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台