首页 > 解决方案 > 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()  ;

    }
}
}

标签: java

解决方案


这是使用基本 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);

推荐阅读