java - Is there a more easy and more than 6 functional code for this?
问题描述
I was trying to do a calculator for my lesson notes and akts system. But I couldn't do a functional option for choosable lesson and akts quantity with for loop.
package sa;
import java.util.Scanner;
class sA {
public static void main(String[] args){
Scanner ders = new Scanner(System.in);
System.out.print("Ders sayısını giriniz: ");
int y = ders.nextInt();
if (y == 6) {
System.out.println("Notları, gireceğiniz akts sırasıyla giriniz!");
Scanner not1 = new Scanner(System.in);
System.out.print("1.not : ");
float q = not1.nextFloat();
Scanner not2 = new Scanner(System.in);
System.out.print("2.not : ");
float a = not2.nextFloat();
Scanner not3 = new Scanner(System.in);
System.out.print("3.not : ");
float z = not3.nextFloat();
Scanner not4 = new Scanner(System.in);
System.out.print("4.not : ");
float w = not4.nextFloat();
Scanner not5 = new Scanner(System.in);
System.out.print("5.not : ");
float s = not5.nextFloat();
Scanner not6 = new Scanner(System.in);
System.out.print("6.not : ");
float x = not6.nextFloat();
System.out.println("Notları girdiğiniz sıra ile, akts lerini giriniz.!");
Scanner akts1 = new Scanner(System.in);
System.out.print("1.akts : ");
int e = akts1.nextInt();
Scanner akts2 = new Scanner(System.in);
System.out.print("2.akts : ");
int d = akts2.nextInt();
Scanner akts3 = new Scanner(System.in);
System.out.print("3.akts : ");
int c = akts3.nextInt();
Scanner akts4 = new Scanner(System.in);
System.out.print("4.akts : ");
int r = akts4.nextInt();
Scanner akts5 = new Scanner(System.in);
System.out.print("5.akts : ");
int f = akts5.nextInt();
Scanner akts6 = new Scanner(System.in);
System.out.print("6.akts : ");
int v = akts6.nextInt();
float t = q*e+a*d+z*c+w*r+s*f+x*v;
float g = t/(e+d+c+r+f+v);
System.out.println(g);
}
else {
System.out.print("Sadece 6 dersi hesaplayacak algoritma bulunmaktadır.");
}
}
}
Here another unfinished try.
package sa;
import java.util.Scanner;
public class es {
public static void main(String[] args) {
System.out.println("Ders sayısını giriniz:");
Scanner ders = new Scanner(System.in);
int y = ders.nextInt();
System.out.println("Notları, gireceğiniz akts sırasıyla giriniz");
for (int i=1; i<=y; i++) {
System.out.println(i+".not");
Scanner s = new Scanner(System.in);
float r = s.nextFloat();
}
for (int a=1; a<=y; a++) {
System.out.println(a+".akts");
Scanner c = new Scanner(System.in);
float e = c.nextFloat() ;
}
}
}
解决方案
这是使用基本 for 循环的示例实现。请注意,我不会说土耳其语(?),因此变量名称可能无法很好地表达其意图。此外,我猜到你的意图是什么。
Scanner scanner = new Scanner(System.in);
// Get number of courses.
System.out.print("Ders sayısını giriniz: ");
int numberOfCourses = scanner.nextInt();
float[] grades = new float[numberOfCourses];
int[] akts = new int[numberOfCourses];
// Get grades.
System.out.println("Notları, gireceğiniz akts sırasıyla giriniz!");
for (int i = 0; i < numberOfCourses; i++)
{
System.out.printf("%d.not : ", i + 1);
grades[i] = scanner.nextFloat();
}
// Get akts.
System.out.println("Notları girdiğiniz sıra ile, akts lerini giriniz.!");
for (int i = 0; i < numberOfCourses; i++)
{
System.out.printf("%d.akts : ", i + 1);
akts[i] = scanner.nextInt();
}
// Calculate result.
float result = 0;
int aktsSum = 0;
for (int i = 0; i < numberOfCourses; i++)
{
result += grades[i] * akts[i];
aktsSum += akts[i];
}
result /= aktsSum;
System.out.println(result);
推荐阅读
- django - NoReverseMatch at /add_post Reverse for 'article-details' with arguments '('4', '0')' 未找到。尝试了 1 种模式:['blogpost/(?P
[^/]+)$'] - amazon-web-services - 如何使用 git 将 nuxt SSR 应用程序部署到 AWS Amplify
- ios - 为 Mac 编译的 ReactNative 应用程序出现错误 AssetsLibrary is deprecated
- python - 如何在条件语句中运行 def 函数?
- javascript - 在 textarea 中使用公共 GitHub raw 的内容
- python - Python:[Errno 30] 只读文件系统:'final_for_sure1.wav.5e3DD1F9
- python - 在 中找不到视频。- DeepLabCut 预训练项目
- html - ReactStrap:下拉项目在手机上显示为透明
- c++ - 对具有智能指针值的映射的读取无效
- ruby-on-rails - link_to_add_association 和 link_to_remove_association 嵌套形式 Cocoon gem