我一直在尝试将数组写入文件.我知道如何将整数或字符串写入文件但是让数组混淆了我.我现在正在使用它:
public static void write (String file, int[]x) throws IOException{
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(filename));
outputWriter.write("hi");// Here I know i cant just write x[0] or anything. Do i need
//to loop in order to write the array?
outputWriter.newLine();
outputWriter.flush();
outputWriter.close();
}
Run Code Online (Sandbox Code Playgroud) 我试图从一个简单的for循环返回一个double.出于某种原因,我不断收到一条错误消息,指出该方法缺少一个return语句.
这是我的代码:
public static double quantitytable(){
for(double x=1; x<=1000; x=x+100){
return x;
}
}
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但我似乎无法弄明白.
如果我正在使用循环并启动一个int,如c为1,我希望它以100为增量显示数字1-2000.
1
100
200
300
400
500
etc.
Run Code Online (Sandbox Code Playgroud)
我会为c = c +写些什么?
我已经尝试了一段时间来弄清楚如何找到随机数组的最大值.这是我正在调试的代码:截至目前,我得到的是数组中的一个值,但它不是最大值.
public static void main(String[] args) {
System.out.println(intOfMaxInRange(randomIntArray(10), 1,30));
}
public static int random(int low, int high){
int x=(int)(Math.random()*high+low);
return x;
}
public static int[] randomArray(int n){
int[] a = new int[n];
for (int i = 0; i<a.length; i++) {
a[i] = randomInt (1,30);
}
return a;
}
public static int intOfMax( int[] array){
int max=array[0];
for(int i=1;i<array.length;i++){
if (array[i] > max) {
}
}
return max;
}
Run Code Online (Sandbox Code Playgroud)