Reputation: 45
I have a question about loops. I am creating a program that calculates the deprecation of an asset(It is unknown), and I am looping it so it runs 4 times. The whole code looks like this
import java.io.*;
import java.util.*;
class Program108b{
public static void main(String[] args){
int [] numbers = {1, 2, 3, 4};
for(int x: numbers){
Scanner kb = new Scanner(System.in);
System.out.print(" Enter price ");
double price = kb.nextDouble();
System.out.print("Enter salvage value ");
double salValue = kb.nextDouble();
System.out.print("Enter estimated life in years ");
int years = kb.nextInt();
double anws = ((price- salValue)/years);
System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
}
}
}
I am trying to make it say "run #1", then "run #2", then "run #3", and then "run #4" at the top of the code when that certain run happens. So right now the output is
Enter price 2500
Enter salvage value 350
Enter estimated life in years 23
Here is the depreciated value: 93.48 Enter price 23
Enter salvage value
32
Enter estimated life in years 3
Here is the depreciated value: -3.0 Enter price 2
Enter salvage value 32
Enter estimated life in years 3
Here is the depreciated value: -10.0 Enter price 23
Enter salvage value 2
Enter estimated life in years 3
Here is the depreciated value: 7.0
(The input is totally random, I just wanted to show what the output is) I want to make the output look like this:
run #1
Enter price 2500
Enter salvage value 350
Enter estimated life in years 23
Here is the depreciated value: 93.48
run #2
Enter price 23
Enter salvage value
32
Enter estimated life in years 3
Here is the depreciated value: -3.0
run #3
Enter price 2
Enter salvage value 32
Enter estimated life in years 3
Here is the depreciated value: -10.0
run #4
Enter price 23
Enter salvage value 2
Enter estimated life in years 3
Here is the depreciated value: 7.0
So my question is- how would I make it say "run #_", and have it change each loop to "run #1", "run #2", etc until the forth run? Also, how would I make the start of a new run begin on a new line (the "enter price" part in this code)?
Upvotes: 0
Views: 66
Reputation: 44
import java.io.*;
import java.util.*;
class Program108b{
public static void main(String[] args){
int [] numbers = {1, 2, 3, 4};
for(int x: numbers){
System.out.print("run #_" + x);
Scanner kb = new Scanner(System.in);
System.out.print(" Enter price ");
double price = kb.nextDouble();
System.out.print("Enter salvage value ");
double salValue = kb.nextDouble();
System.out.print("Enter estimated life in years ");
int years = kb.nextInt();
double anws = ((price- salValue)/years);
System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
}
}
}
or create a variable and add 1 to it on every iteration
import java.io.*;
import java.util.*;
class Test1{
public static void main(String[] args){
int [] numbers = {1, 2, 3, 4};
int iteration = 0;
for(int x: numbers){
System.out.println("run #"+ ++iteration);
Scanner kb = new Scanner(System.in);
System.out.print(" Enter price ");
double price = kb.nextDouble();
System.out.print("Enter salvage value ");
double salValue = kb.nextDouble();
System.out.print("Enter estimated life in years ");
int years = kb.nextInt();
double anws = ((price- salValue)/years);
System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
}
}
}
Upvotes: 0
Reputation: 570
Try this -
import java.io.*;
import java.util.*;
class Program108b {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4};
for(int x: numbers){
System.out.println("run #" + x); /// Add this
Scanner kb = new Scanner(System.in);
System.out.print(" Enter price ");
double price = kb.nextDouble();
System.out.print("Enter salvage value ");
double salValue = kb.nextDouble();
System.out.print("Enter estimated life in years ");
int years = kb.nextInt();
double anws = ((price- salValue)/years);
System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
}
}
}
Upvotes: 0
Reputation: 26926
Simply add a System.out.println
at the beginning of loop
for (int x: numbers) {
System.out.println("run #" + x);
// Rest of code
}
Upvotes: 4