gpt4 book ai didi

java - java中数组中的不兼容类型

转载 作者:行者123 更新时间:2023-11-30 03:14:14 25 4
gpt4 key购买 nike

我对java很陌生,对于作业,我必须要求三个单词,然后在数组中逐列垂直打印它们。我想我已经走了很远,但由于不兼容的类型问题,我无法打印数字。错误是针对 array[numb1][0]= word1.charAt(numb1); 给出的。 java 不接受 numb1,我该如何解决这个问题?

import java.util.Scanner;
import java.util.Arrays;
import java.lang.String;

public class assignment51
{
static void main()
{
Scanner read=new Scanner(System.in);
System.out.println("Please enter the first of three words:");
String word1= read.nextLine();

System.out.println("Please enter the second of three words:");
String word2= read.nextLine();

System.out.println("Please enter the third of three words:");
String word3= read.nextLine();

int count1 = word1.length();
int count2 = word2.length();
int count3 = word3.length();


int [] nums = new int [] {count1,count2,count3};
int max = 0;
for (int i = 0;i<nums.length;i++)
{
if (nums[i] >max)
{
max=nums[i];
}
}

max=max-1;
String [][] array=new String[max][2];

for (int numb1 = 0; numb1<(count1-1); numb1++)
{
array[numb1][0]= word1.charAt(numb1);
}

for (int numb2 = 0; numb2<(count2-1); numb2++)
{
array[numb2][1]= (word2.charAt(numb2));
}

for (int numb3 = 0; numb3<(count3-1); numb3++)
{
array[numb3][2]= (word3.charAt(numb3));
}


}
}

最佳答案

您正在尝试将一个字符分配给一个字符串。那是行不通的。在 Java 中,字符和字符串是完全不同的动物。

您的数组应声明为char[][]

关于java - java中数组中的不兼容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com