在java中使用scanner类进行输入
我正在制作一个程序,将给定的整数减少到其最简单的比率。但是,在程序的子方法中通过Scanner类获取输入时,出现了一个错误。下面是代码:在java中使用scanner类进行输入,java,java.util.scanner,Java,Java.util.scanner,我正在制作一个程序,将给定的整数减少到其最简单的比率。但是,在程序的子方法中通过Scanner类获取输入时,出现了一个错误。下面是代码: package CodeMania; import java.util.Scanner; public class Question5 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int T=sc.nextInt();// nu
package CodeMania;
import java.util.Scanner;
public class Question5
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();// number of test cases
sc.close();
if(T<1)
{
System.out.println("Out of range");
System.exit(0);
}
for(int i=0;i<T;i++)
{
ratio();//line 19
}
}
static void ratio()
{
Scanner sc1=new Scanner(System.in);
int N=sc1.nextInt();//line 26
if((N>500)||(N<1))
{
System.out.println("Out of range");
System.exit(0);
}
int a[]=new int[N];
for(int i=0;i<N;i++)
{
a[i]=sc1.nextInt();
}
int result = a[0];
for(int i = 1; i < a.length; i++)
{
result = gcd(result, a[i]);
}
for(int i=0;i<N;i++)
{
System.out.print((a[i]/result)+" ");
}
sc1.close();
}
static int gcd(int a, int b)
{
while (b > 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
在这里,我在主功能中使用了两个独立的扫描仪对象sc,在比率功能中使用了sc1,以从控制台获取输入。
但是,如果我在类作用域中声明了一个公共静态类型的Scanner对象,然后在整个程序中只使用一个Scanner对象来获取输入,那么程序将按照要求正常工作
发生这种情况的原因…?此错误的原因是,在扫描仪上调用.close()也会关闭inputStream系统。在中,但实例化新扫描仪不会重新打开它 您需要在方法参数中传递一个扫描器,或者使其成为一个静态全局变量。由于您的
main()
和ratio()
方法使用扫描器,它们会抛出异常,当异常发生时,程序的正常流程会中断,程序/应用程序会异常终止,不建议这样做,因此应处理这些例外情况。
异常可能由于许多不同的原因发生,下面给出了发生异常的一些场景
A user has entered invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or the JVM has run out of memory.
您可以使用Try/Catch块来处理这些异常,也可以在方法定义后使用单词throws来处理这些异常,
在您的案例中,这两种方法如下:
使用Try/Catch:
public static void main()
{
try{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();// number of test cases
sc.close();
}
catch(NoSuchElementException e){
System.out.print("Exception handled" + e);
//rest of method
}
static void ratio(){
try{
Scanner sc1=new Scanner(System.in);
int N=sc1.nextInt();}
catch(NoSuchElementException e){
System.out.print("Exception handled" + e);}
//rest of method
}
带有“投掷”:
public static void main()throws Exception{
//rest of method
}
static void ratio()throws Exception
{
//rest of method
}
试试这个。您可以将扫描仪作为参数传递
package stack.examples;
import java.util.Scanner;
public class Question5 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();// number of test cases
if (T < 1) {
System.out.println("Out of range");
System.exit(0);
}
for (int i = 0; i < T; i++) {
ratio(sc);// line 19
}
sc.close();
}
static void ratio(Scanner sc1) {
int N = sc1.nextInt();// line 26
//Your Logic
}
static int gcd(int a, int b) {
while (b > 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
package stack.examples;
导入java.util.Scanner;
公开课问题5{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int T=sc.nextInt();//测试用例数
if(T<1){
System.out.println(“超出范围”);
系统出口(0);
}
for(int i=0;i0){
内部温度=b;
b=a%b;
a=温度;
}
返回a;
}
}
现在,为了解释这一点,我们必须从Java实用程序包中导入一个scanner类,这样就可以通过第一行的代码实现
第二行是创建一个类注释(类的名称不必以大写字母开头),现在来到主主题Scanner类,因此为此,我们必须使用第四行中给出的代码在程序中创建一个Scanner类。。。在此语句中,“sc”是一个存储scanner类值的对象,因此,如果您想在scanner类中执行任何操作,您可以通过对象“sc”*注意(您可以将您的对象命名为任何名称,例如:poop、bla等)。。。
然后我们有一个有趣的命令,上面写着System.in,现在它允许用户在运行时通过键盘或任何这样的输入设备写入任何语句。。。。
String name=sc.next()这一行帮助我们编写在运行时要编写的任何字符串,这些字符串将存储在name变量中
就这样,这是美国的扫描器类。希望它容易理解
干杯!!继续编码:-)看,我想你也有同样的问题。我知道整个程序需要通过一个扫描仪。但我不知道为什么我不能使用多个扫描仪……谢谢你的原因。
package stack.examples;
import java.util.Scanner;
public class Question5 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();// number of test cases
if (T < 1) {
System.out.println("Out of range");
System.exit(0);
}
for (int i = 0; i < T; i++) {
ratio(sc);// line 19
}
sc.close();
}
static void ratio(Scanner sc1) {
int N = sc1.nextInt();// line 26
//Your Logic
}
static int gcd(int a, int b) {
while (b > 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
import java.util.*;
public class Understanding_Scanner
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Please enter your name");
String name=sc.next();
System.out.println("Your name is:"+name);
}
}