[英]java variables for whole package
我是Java的新手,我不知道如何制作它,因此if語句中的這兩個變量每次都需要更新。
public class Ex3 {
public static void main(String[] args) {
//Here will display what pi is
System.out.println("Pi in Netbeans Java is "+ Math.PI);
//This will give how many numbers the for loops go up to
int maxNum = 20;
//This is for the subtraction parts of the equation
for(int i = 1; i <= maxNum; i++){
int subNum = 3;
final double subCount = 1 - (1./(subNum + 4));
subNum += 4;
}
for(int y = 1; y <= maxNum; y++){
int addNum = 5;
double addCount = 1 + (1./(addNum + 4));
addNum += 4;
}
}
double finalNum = subCount + addCount;
}
變量a僅限於在其中聲明的范圍,因此,如果在for
塊中聲明變量,則只能在for
塊中看到它們。
為了克服這個問題,請在使用main
方法之前先聲明變量
public static void main(String[] args) {
//Here will display what pi is
System.out.println("Pi in Netbeans Java is "+ Math.PI);
//This will give how many numbers the for loops go up to
int maxNum = 20;
double subCount = 0.0;
double addCount = 0.0;
int subNum = 3;
int addNum = 5;
//This is for the subtraction parts of the equation
for(int i = 1; i <= maxNum; i++){
subCount = 1 - (1./(subNum + 4));
// maybe should be
// subCount += 1 - (1./(subNum + 4));
subNum += 4;
}
for(int y = 1; y <= maxNum; y++){
addCount = 1 + (1./(addNum + 4));
// maybe should be
// addCount += 1 + (1./(addNum + 4));
addNum += 4;
}
System.out.println ("the result is " + (subCount + addCount));
}
編輯
雖然不確定您的邏輯應該做什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.