如何打印三角形的数量然后打印它的最小周长?
问题描述:
import java.util.Scanner;
public class project {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
// to scan the values
int N ;
// numbers of lines
double side1,side2,side3; `
double minimum = 1000;
// to keep the minimum value
double sum = 0;
// to keep calculated values
int which_one = 0 ;
System.out.println("How many triangles do you have?");
N = input.nextInt();
// how many lines
System.out.println("Please, insert lengths of the "
+ "sides of these triangles (3 real numbers per line):");
for (int i = 0; i < N ; i++){
// loop for taking the value from the user
side1 = input.nextDouble(); // input of side1
side2 = input.nextDouble(); // input of side2
side3 = input.nextDouble(); // input of side3
sum = side1 + side2 + side3;
if (sum < minimum) minimum = sum;
which_one = N+1;
}
System.out.printf("Triangle no."+ which_one
+ " has the minimum perimeter which is %.1f%n "+minimum);
}
}
答
您的错误在这一行。我已经修正它到它应该是什么:
System.out.printf("Triangle no." + which_one +
" has the minimum perimeter which is %.1f\n " , minimum);
我minimum
与之前更换了+
“”让minimum
被作为格式说明%.1f
值提供。
这里有问题吗?你的代码是否工作?你要求我们做什么? –
欢迎来到Stack Overflow!看起来你正在寻求作业帮助。虽然我们本身没有任何问题,但请观察这些[应做和不应该](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),并相应地编辑您的问题。 –