Hier ist mein aktueller Code ist:Code zu ändern, eine andere Antwort zu bekommen
import java.util.Scanner;//importing scanner
public class QuestionOne {
public static void main(String[] args) {
int numberofDays;//these two lines define variables
int sharePoints;
Scanner keyboard = new Scanner(System.in);//activating scanner
System.out.print("Number of days in the period: ");//asking question
numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input
System.out.print("Share points on the first day: ");//asking another question
sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input
numberofDays = validator(numberofDays,keyboard); // Validates Keyboard input
//above two lines print day and share points, as well as the first line of text (as it does not change)
outPutTablePrinter(numberofDays,sharePoints);
}
private static void outPutTablePrinter(int numberOfDays,int sharePoints){
System.out.println("Day " + " Share Points");
System.out.println(1 + " " + sharePoints);
for (int i = 2; i <= numberOfDays; i++) {
if (numberOfDays % 2 == 0)
if (i <= numberOfDays/2) {
sharePoints = sharePoints + 50;
System.out.println(i + " " + sharePoints);
} else {
sharePoints = sharePoints - 25;
System.out.println(i + " " + sharePoints);
} else {
if (i <= numberOfDays/2 + 1) {
sharePoints = sharePoints + 50;
System.out.println(i + " " + sharePoints);
} else {
sharePoints = sharePoints - 25;
System.out.println(i + " " + sharePoints);
// above nested if else statements essentially calculate
and concatenate the variables to obtain an answer that is then printed
and repeated until the number of days is reached (starting from day two)
}
}
}
}
private static int validator(int numberOfDays,Scanner keyboard){
while (numberOfDays < 10 || numberOfDays > 20)//while loop makes sure
the conditions stay true
{
System.out.println("The number of days doesn’t meet the required
criteria, enter it again");
System.out.print("Number of days in the period: ");
numberOfDays = keyboard.nextInt();
}
return numberOfDays;
}
}
Was der Code derzeit tut, ist für die Tage der Periode stellen und dem ursprünglichen Preises und dann zeigt es den Preis für jeden Tag bis der letzte in der Periode. Zum Beispiel setzen, wenn ich 11 Tage als meine Periode und 550 als mein ersten Bestandswert dann würde ich folgende:
Day Share Points
1 550
2 600
3 575
4 625
5 600
6 650
7 625
8 675
9 650
10 700
11 675
Was ich stattdessen tun will, ist es so ändern, die endgültige Antwort (wenn die gleichen Werte eingegeben werden), wie folgt aussehen sollte:
The share points on the final day would be: 675
Was das Problem, das Sie haben Begegnung ist und wie hast du versucht, es zu lösen? – pvg
Sie möchten also Text zur Ausgabezeichenfolge hinzufügen. Haben Sie versucht, zu der Codezeile zu gehen, die diese Zeichenfolge ausgibt und dort den Text eintippt? – David