55.6k views
1 vote
Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores

User Raeez
by
7.1k points

1 Answer

0 votes

Answer:

Step-by-step explanation:

The following is written in Java. It is a method/function that takes the oldScore as an int input. It turns it into a String, separates all the digits, puts them into an array. Then adds the first digit to the end and removes it from the beginning, thus shifting the entire score one to the left. Then it turns the String variable back into an Integer called finalScore and prints it out.

public static void newScore(int oldScore) {

ArrayList<Integer> newScore = new ArrayList<>();

String stringOldScore = String.valueOf(oldScore);

for (int x = 0; x < stringOldScore.length(); x++) {

newScore.add(Integer.parseInt(String.valueOf(stringOldScore.charAt(x))));

}

newScore.add(newScore.get(0));

newScore.remove(0);

String newScoreString = "";

for (int x:newScore) {

newScoreString += String.valueOf(x);

}

int finalScore = Integer.parseInt(newScoreString);

System.out.println(finalScore);

}

User Jealie
by
6.6k points