Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

عندما أدخل أرقام عشرية فإن الكود يتوقف!

السلام عليكم و رحمة الله و بركاته.

في المثال الثاني من درس إدخال البيانات قمت بتطبيق جميع الخطوات الواردة في الشرح و لكن عند تشغيل الكود لاحظت أنني عند إدخال أرقام صحيحة يعمل الكود بشكل صحيح و لكن عندما أدخل أرقام عشرية فإن الكود يتوقف و تخرج الرسالة التالية:

C:\Users\DAHER\.jdks\openjdk-18.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\lib\idea_rt.jar=50468:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\DAHER\IdeaProjects\Practica1\Harmash.com\out\production\Ejercicios.H com.imput_1.Notes
Enter the notes of the following methods
Java: 8.6
java.util.InputMismatchException
Process finished with exit code 0
C:\Users\DAHER\.jdks\openjdk-18.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\lib\idea_rt.jar=50468:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\DAHER\IdeaProjects\Practica1\Harmash.com\out\production\Ejercicios.H com.imput_1.Notes Enter the notes of the following methods Java: 8.6 java.util.InputMismatchException Process finished with exit code 0

و هنا أرفق الكود كما طبقته وفقاً للشرح الوارد في المثال:

import java.util.Scanner;
public class Notes {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
float[] notes = new float[5];
float avg;
System.out.println("Enter the notes of the following methods ");
try{
System.out.print("Java: ");
notes[0] = input.nextFloat();
System.out.print("HTML: ");
notes[1] = input.nextFloat();
System.out.print("PHP: ");
notes[2] = input.nextFloat();
System.out.print("SQL: ");
notes[3] = input.nextFloat();
System.out.print("Linux: ");
notes[4] = input.nextFloat();
avg = (notes[0] + notes[1] + notes[2] + notes[3] + notes[4]) / 5;
if (avg >= 10) {
System.out.println("Congratulatio, your average is: " + avg);
}
else{
System.out.println("Sorry, you fail! your averg is: " + avg);
}
}
catch (Exception e) {
System.out.print(e.toString());
}
finally{
input.close();
}
}
}
import java.util.Scanner; public class Notes { public static void main(String[] args){ Scanner input = new Scanner(System.in); float[] notes = new float[5]; float avg; System.out.println("Enter the notes of the following methods "); try{ System.out.print("Java: "); notes[0] = input.nextFloat(); System.out.print("HTML: "); notes[1] = input.nextFloat(); System.out.print("PHP: "); notes[2] = input.nextFloat(); System.out.print("SQL: "); notes[3] = input.nextFloat(); System.out.print("Linux: "); notes[4] = input.nextFloat(); avg = (notes[0] + notes[1] + notes[2] + notes[3] + notes[4]) / 5; if (avg >= 10) { System.out.println("Congratulatio, your average is: " + avg); } else{ System.out.println("Sorry, you fail! your averg is: " + avg); } } catch (Exception e) { System.out.print(e.toString()); } finally{ input.close(); } } }

و لكم جزيل الشكر و التقدير.

تعليقات 1

أضف تعليق

يجب تسجيل الدخول حتى تتمكن من إضافة تعليق أو رد.