[英]Saving local variables - Java
Here is a program that is supposed to find out whether a sequence of numbers match the mathematical formula a[n+1] = a[n]*b+c for any combination of b and c in the integer range -9:9. 这是一个程序,该程序应查找整数范围-9:9中b和c的任意组合的数字序列是否与数学公式a [n + 1] = a [n] * b + c相匹配。
import java.util.Scanner;
public class Nastaord{
private static int[] lasTal(){
int[] tallista; //Det vi ska ha talföljden i
int i = 0; //räknare för tallista
while(true){
System.out.print("Ange tal, eller tryck enter om du är klar: ");
int nytt_tal = scanner.nextLine();
if(nytt_tal == ""){
return tallista;}
tallista[i] = nytt_tal;
i++;
}
}
private static boolean bcFinns(int[] tallista){
boolean OK = true;
for(int b = -9; b <= 9; b++){
for(int c = -9; c <= 9; c++){
for(int i = tallista.length; i > 0;i--){
OK = tallista[i] == tallista[i-1]*b+c;
if(OK == false){
break;}
}
if(OK == true){
public int b = b;
public int c = c;
return true;}
}
}
return false;
}
public static void main(String[] args){
boolean OK = bcFinns(lasTal());
if (OK == true){
System.out.print(tallista[tallista.length-1]*b+c);
}
if (OK == false){
System.out.print("No");
}
}
}
The program, on a principal level, works. 该程序在主要级别上起作用。 The only thing is that I do not know how to save the correct numbers b and c for the sequence once they are found. 唯一的问题是,一旦找到正确的数字b和c,我将不知道如何保存它们。 I tried creating two public variables so that I can access them in the main method, but I get the following error: 我尝试创建两个公共变量,以便可以在main方法中访问它们,但是出现以下错误:
Nastaord.java:30: error: illegal start of expression
public int b = b;
^
Nastaord.java:31: error: illegal start of expression
public int c = c;
Could you help me save these variables b and c in some way? 您能以某种方式帮助我保存这些变量b和c吗?
public class Nastaord{
public static int bFinal,cFinal;
Then later on: 然后稍后:
bFinal = b;
cFinal = c;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.