Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何输出代码正在运行的循环迭代_Java_Loops - Fatal编程技术网

Java 如何输出代码正在运行的循环迭代

Java 如何输出代码正在运行的循环迭代,java,loops,Java,Loops,我有一个关于循环的问题。我正在创建一个计算资产折旧的程序(未知),我正在循环它,使它运行4次。整个代码如下所示 import java.io.*; import java.util.*; class Program108b{ public static void main(String[] args){ int [] numbers = {1, 2, 3, 4}; for(int x: numbers){ Scanne

我有一个关于循环的问题。我正在创建一个计算资产折旧的程序(未知),我正在循环它,使它运行4次。整个代码如下所示

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));
         }
     }
 }
我试图让它在发生特定运行时,在代码顶部显示“run#1”,然后是“run#2”,然后是“run#3”,然后是“run#4”。所以现在的输出是

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
(输入完全是随机的,我只是想显示输出是什么) 我想使输出如下所示:

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

所以我的问题是-我如何让它说“run####并让它在第四次运行之前将每个循环更改为“run#1”、“run#2”等?另外,如何使新运行的开始从新行开始(此代码中的“输入价格”部分)?

只需在循环开始处添加一个
System.out.println

for (int x: numbers) {
    System.out.println("run #" + x);
    // Rest of code
}

只需在循环的开头添加一个
System.out.println

for (int x: numbers) {
    System.out.println("run #" + x);
    // Rest of code
}
试试这个-

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));
    }
  }
}
试试这个-

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));
    }
  }
}

System.out.print不添加新行。System.out.println有。也可以在打印字符串中添加“\n”以手动添加新行。这会帮助你清理东西。System.out.print不会添加新行。System.out.println有。也可以在打印字符串中添加“\n”以手动添加新行。这会帮你清理干净的。谢谢。但是我该如何开始新的节目呢?谢谢。但我如何在新的线路上启动该程序?