我想知道如何打印出一个字符串“Hello index:”,然后在每一行打印出一个值为array2的值,从值1开始,然后在每个索引上加上5 1 ...如果有人可以提供帮助,我们将不胜感激。我是java的初学者,但我知道基于数组的基础知识。在我做数组之前,我学习了对象并使用了对象。我的问题是如何从每个数组中获取要打印出来的值。我如何设置println来打印这些值。非常感谢你!
public class tuna {
private String[] array1;
private int[] array2;
public tuna(String[] array1, int[] array2){
this.array1 = array1;
this.array2 = array2;
}
int i, k;
public String[] getArray1(){
return array1;
}
public int[] getArray2(){
return array2;
}
}
public static void main(String[] args) {
String[] array1 = new String[10];
int[] array2 = new int[10];
array2[0] = 1;
tuna me = new tuna(array1, array2);
for(int i = 0; i < array1.length; i++){
array1[i] = "Hello index: ";
}
for(int x = 1; x<array2.length - 1; x++){
change(array2);
array2[x] = array2[x-1];
if(array2[x+1] > array2.length)
array2[x+1] = array2[x];
}
me.getArray1();
me.getArray2();
???的System.out.println(); ..........
答案 0 :(得分:0)
我不确定你的意思,但如果你想从字符串数组中打印出一个值,你可以这样做:
public class New {
public static void main(String[] args) {
//makes array, and 10 is how many slots it will have
String[] array1 = new String[10];
//sets slot 1 to "Hello"
array1[1] = "Hello";
//prints out slot 1
System.out.println(array1[1]);
}
}
答案 1 :(得分:0)
public class tuna
{
private String[] array1;
private int[] array2;
public tuna(String[] array1, int[] array2)
{
this.array1 = array1;
this.array2 = array2;
}
public String[] getArray1()
{
return array1;
}
public int[] getArray2()
{
return array2;
}
}
public static void main(String[] args)
{
String[] array1 = new String[10];
int[] array2 = new int[10];
array2[0] = 1;
tuna me = new tuna(array1, array2);
for(int i = 0; i < array1.length; i++)
{
array1[i] = "Hello index: ";
}
for(int i = 1; i < array2.length; i++)
{
change(array2);
array2[i] = array2[i-1] + 5;
}
for(int i = 0; i < array2.length; i++)
{
System.out.println (array1[i] + array2[i]);
}
}
但请注意,对于同一消息,您不需要String数组,即Hello索引: