ืืื ืฉืืืช nextInt() ื-Java?
ืืืืื 1
ืืืื ื ืืงื ืืช ืืฆืืืื ืืจืืฉืื ื ืฉืื ื ืืืืืื ืืืกืืกืืช.
import java.util.Scanner;
public class TestIntInput {
public static void checkInt(String testData) {
System.out.println(testData);
Scanner scanner = new Scanner(testData);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
// calling the nextInt() method
System.out.println(scanner.nextInt() + "\t\t INT FOUND!");
} else {
System.out.println(scanner.next() + "\t\t");
}
}
scanner.close();
System.out.println();
}
public static void main(String[] args) {
String testData1 = "My 3 years old cat Diana, just gave birth to 5 healthy babies.";
String testData2 = "The number 1 place to learn Java is CodeGym!";
String testData3 = "6; 5 4 3. 2 1 !";
checkInt(testData1);
checkInt(testData2);
checkInt(testData3);
}
}
ืชึฐืคืึผืงึธื
ืืืชืืื ืฉืื ืืืื ื ืืช ื-3, ืืืื ืื ืขืชื 5 ืชืื ืืงืืช ืืจืืืื. 3 INT ืฉืื ื ืืฆืื! ืืชืืื ืืช ืฉื ืื ืืืื ื, ืืืื ืื ืขืชื 5 INT FOUND! ืชืื ืืงืืช ืืจืืืื. ืืืงืื ืืกืคืจ 1 ืืืืื Java ืืื CodeGym! ืืกืคืจ 1 INT FOUND! ืืืงืื ืืืืื Java ืืื CodeGym! 6; 5 4 3. 2 1 ! 6; 5 INT ื ืืฆืื! 4 INT ื ืืฆืื! 3. 2 INT ื ืืฆืื! 1 INT ื ืืฆื! !
ืึถืกืึผึตืจ
ืืืจ ืืื ืฉืืฉ ืืฆืืื ืืืืืื ืฉืืขืื ื- testData3 ืืื ืฉืืกืคืจ ืฆืจืื ืืืืืช ืืืคืจื ืืจืืื ืืื ืืืืกืจืง ื-int ืืืื. ืื ืืกืืื ืืืืข 6 ื-3 ืืื ื ืืืืืื ืืืกืคืจืื ืฉืืืื ืืืืืื ืฉืื ืืืคืจืืื ืื ืงืืืชืืื ืืืคืกืืงืื ืืืชืืื.ืืืืื 2
ืืืืื ืื ืืฉืชืืฉืช ืืงืื ืืืขืจืืช ืืื ืืกืจืืง ืืืชื ืืืกืคืจืื ืฉืืืื.
import java.util.Scanner;
public class TestSystemInput {
public static void getFinalExamScore() {
System.out.println("Get Your Final Exam Score!\n");
int finalScore = 0;
int totalCourses = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter total Courses: ");
totalCourses = scanner.nextInt();
for (int i = 0; i < totalCourses; i++) {
System.out.println("Enter score in course " + (i + 1) + " : ");
finalScore = finalScore + scanner.nextInt();
}
System.out.println("Your final Score = " + finalScore);
scanner.close();
}
public static void main(String[] args) {
getFinalExamScore();
}
}
ืชึฐืคืึผืงึธื
ืงืื ืืช ืฆืืื ืืืืื ื ืืกืืคืืช ืฉืื! ืืื ืก ืกื"ื ืงืืจืกืื: 3 ืืื ืฆืืื ืืงืืจืก 1 : 10 ืืื ืฆืืื ืืงืืจืก 2 : 15 ืืื ืฆืืื ืืงืืจืก 3 : 15 ืืฆืืื ืืกืืคื ืฉืื = 40
GO TO FULL VERSION