2013年10月31日星期四

Why scanner has been thrown after this cycle ah

import java.io. *;
import java.util.InputMismatchException;
import java.util.Scanner;

public class basicType {

/ **
* @ param args
* /

public static void S_equals ()
{
String s1 = "abc", s2 = "abc";
System.out.println ("s1 equals s2 is" + s1.equals (s2));
System.out.println ("s1 equals \" abc \ "is" + s1.equals ("abc"));
System.out.println ("\" abc \ "equals \" abc \ "is" + "abc". equals ("abc")) ;
}

public static void N_scan ()
{
int sum = 0;
Scanner input = new Scanner (System.in);
System.out.print ("input 10 interger number:");
for (int i = 0; i <10; i + +)
{
int m = 0;
try {
m = input.nextInt ();
}
catch (InputMismatchException e) / / loop has been in this place seeking to explain ah
{
i -;
}
sum + = m;
}
System.out.println ("sum of the 10 Intergers is:" + sum);
}

public static void main (String [] args) {
/ / TODO Auto-generated method stub
S_equals ();
N_scan ();
}

}

------ Solution ------------------------------------ --------
you want users to the number of one-time output 10 Mody ? The logic that you are definitely wrong ah

give you a first input 10 the number of bars, 10 numbers separated by a comma when entered in accordance with the


import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;

public class BasicType {



    public static void S_equals()
    {
        String s1="abc",s2="abc";
        System.out.println("s1 equals s2 is "+s1.equals(s2));
        System.out.println("s1 equals \"abc\" is "+s1.equals("abc"));
        System.out.println("\"abc\" equals \"abc\" is "+"abc".equals("abc"));
    }

    public static void N_scan()
    {
        int sum=0;
        Scanner scanner=new Scanner(System.in);
        System.out.print("input 10 interger number like 1,2,3 ...");
        String inputStr=scanner.next();
        String[] numStr=inputStr.split(",");
        for(String s:numStr){
             int value=Integer.parseInt(s);
             sum=sum+value;
        }
//        for(int i=0;i<10;i++)
//        {
//            int m=0;
//            try{
//                m=input.nextInt();
//            }
//            catch(InputMismatchException e) //一直在这个地方循环 求解释啊
//            {
//                i--;
//            }
//            sum+=m;
//        }
        System.out.println("sum of the 10 Intergers is:"+sum);
    }

    public static void main(String[] args) {
// TODO Auto-generated method stub
        S_equals();
        N_scan();
    }

}


------ Solution ------------------------------------- -------
try {
m = input.nextInt ();
}
catch (InputMismatchException e) / / loop has been in this place seek to explain ah
{
i -;
} sum + = m;
In sum + = m; few months before " ; "
can be added
try {
m = input.nextInt ();
}
catch (InputMismatchException e) / / loop has been in this place seek to explain ah
{
i -;
};
sum + = m;

------ Solution ------------------------------------ --------
Scanner delimiter mode using its input into tokens , so when you enter a tag with the pattern does not match the expected type occurs when InputMismatchException exception in your program for loop in the event of an anomaly, an exception will occur next time , think your input object or the object that the exception occurred ; would have been circulating as to why , it is because your program when an exception occurs i -, this is just the for loop i + + offset into an infinite loop . I tell you the program i - understanding is : When the input character and the expected type does not match mode , re-enter until the input type matching characters. So here is the program modifications:
for (int i = 0; i < 10; i++) {
int m = 0;
try {
m = input.nextInt();
} catch (InputMismatchException e) // 一直在这个地方循环 求解释啊
{
input = new Scanner(System.in);//当发生异常时,new一个新的Scanner对象
i--;
}
sum += m;
}

------ For reference only ----------------------------------- ----
nextLine () can each lose an integer knock one enter
------ For reference only ----------------- ----------------------
I entered a non-integer enter into an infinite loop will be sure to read the integer is not an integer then toss abnormal repeats the last read an integer

------ For reference only ---------------------------------- -----
have given you an example posted , did not see it ?


------ For reference only ---------------------------------- -----
examples need not because I do not want to hurry
------ For reference only --------------- ------------------------
you write this example would logically not normal , how do you make a sudden importer enter the numbers ?
separated by spaces ? program which specifically consider how you achieve it ?


------ For reference only ---------------------------------- -----
I use your code , how wrong it is not what you say :

in dos compile, run right
------ For reference only ---------------------- -----------------
you enter 10 integers it is right

------ For reference only ---------------------------------- -----
asked me to use
Scanner input = new Scanner (System.in);
hasNextInt () to determine whether the integer
if not how to use input.nextInt () reads the next few
------ For reference only -------------------- -------------------
will throw an exception if the direct reading
------ For reference only --------- ------------------------------

like the following passage from the first execution after each loop will execute the following period , it has been impossible to escape cycle

catch(InputMismatchException e) //一直在这个地方循环 求解释啊
{
i--;


------ For reference only ----------------------------------- ----
scanner very tasteless questions asked no way ah tangle me a few hours to continue efforts
------ For reference only ------ ---------------------------------
positive solution I have just found the answer
import java.io. *;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class basicType {

/ **
* @ param args
* /

public static void S_equals ()
{
String s1 = "abc", s2 = "abc";
System.out.println ("s1 equals s2 is" + s1.equals (s2));
System.out.println ("s1 equals \" abc \ "is" + s1.equals ("abc"));
System.out.println ("\" abc \ "equals \" abc \ "is" + "abc". equals ("abc")) ;
}

public static void N_scan ()
{
int sum = 0;
for (int i = 0; i <10; i + +)
{
int m = 0;
boolean flag = true;
System.out.print ("input an interger:");
while (flag)
{
Scanner input = new Scanner (System.in);
try
{
m = input.nextInt ();
flag = false;
}
catch (java.util.InputMismatchException e)
{
System.out.print ("wrong! not an interger! enter again:");
flag = true;
}
}
sum + = m;
}
System.out.println ("sum of the 10 intergers is:" + sum);
}

public static void main (String [] args) {
/ / TODO Auto-generated method stub
S_equals ();
N_scan ();
}

}

------ For reference only ---------------------------------- -----
try {
m = input.nextInt ();
}
catch (InputMismatchException e) / / loop has been in this place seek to explain ah
{
i -;
}
because every time you have done i - operation, if non-compliance with a number ( ie not here to type int ), nextInt method will always stay at this number does not match , while throwing abnormal , this time i have been doing i - operation , in a for loop just i + +, and now i -, equivalent to i has not changed, of course, would have been circulated
landlord should be logical errors , the i - deleted or changed on ok
------ For reference only ---------------- -----------------------
throw InputMismatchException when a scanner , the scanner will not pass mark that caused the exception , so you can get or some other method to skip it .
This is the java API documentation of the original words . This means that , if the method does not successfully resolve nextInt an integer , then your input will not be ignored. Because it might be resolved in other formats .
Then you enter a program , there was an exception , number 100 is assigned to begin the next cycle , but this time the data buffer you last entered a still, and not cleared , then continue parsing a, or wrong , so this process has been repeated anymore .
now you want to change is the input of the error clears , as long as the catch , add one : input.next () to get it !
Scanner can also be written in a try inside
------ For reference only -------------------------- -------------
speak great. . .
------ For reference only -------------------------------------- -



specially registered a number of points a praise

没有评论:

发表评论