我试图输入数组的长度,然后输入数组元素。我能够检查案例1中的最大值但是我将案例2重新排序从最小数字到最大数字,但我无法做到。
这是我的代码:
package peopleinfo;
import java.util.Scanner;
public class mainapp
{
public static void main(String[] args)
{
Scanner sc1 = new Scanner(System.in);
System.out.println("please input the length of the array:");
arraybuilder(sc1.nextInt());
}
public static void arraybuilder(int i)
{
Scanner sc = new Scanner(System.in);
int[] array = new int[i];
for (int j = 0; j < array.length; j++)
{
int o = j;
System.out.println("please input the" + ++o + " number");
array[j] = sc.nextInt();
}
for (int j2 = 0; j2 < array.length; j2++)
System.out.print(array[j2] + "\t");
System.out.println("what do you want to do ?\n 1 check the biggest number\n2 sort the array from the smallest\nterminate the app");
int dwc = sc.nextInt();
switch (dwc)
{
case 1:
int k = 0;
int max = 0;
for (int j = 0; j < array.length; j++)
{
if (array[j] > k)
{
max = j;
k = array[j];
}
}
System.out.println("the largest number is " + array[max]);
break;
case 2:
int temp;
boolean fixed = false;
while (fixed = false)
{
fixed = true;
for (int u = 0; u < array.length - 1; u++)
{
if (array[u] > array[u + 1])
{
temp = array[u + 1];
array[u + 1] = array[u];
array[u] = temp;
fixed = false;
}
}
}
for (int j = 0; j < array.length; j++)
{
System.out.print(array[j] + "\t");
}
break;
default:
System.out.println("Error, please rerun the code:)");
break;
}
}
}
我不明白出错了什么?
答案 0 :(得分:1)
case 2:
int temp;
boolean fixed = false;
while (fixed == false) { // See the change here
fixed = true;
for (int u = 0; u < array.length - 1; u++) {
if (array[u] > array[u + 1]) {
temp = array[u + 1];
array[u + 1] = array[u];
array[u] = temp;
fixed = false;
}
}
}
for (int j = 0; j < array.length; j++) {
System.out.print(array[j] + "\t");
}
break;
在您的案例2中,while()
循环有fixed = false
应该是fixed == false
,即while(fixed==false)
。
有关详细信息,请点击链接... Using the assignment operator instead of the equality operator
答案 1 :(得分:0)
您将值false分配给fixed而不是compare.Change代码如下,并检查它是否应该工作。
case 2:
int temp;
boolean fixed = false;
while (fixed == false) { //check this line
fixed = true;
for (int u = 0; u < array.length - 1; u++) {
if (array[u] > array[u + 1]) {
temp = array[u + 1];
array[u + 1] = array[u];
array[u] = temp;
fixed = false;
}
}
}
for (int j = 0; j < array.length; j++) {
System.out.print(array[j] + "\t");
}
break;
答案 2 :(得分:0)
您已将boolean
检查为while (fixed = false)
。请注意,您要在那里分配值,并检查boolean
是否需要使用==
或while (fixed == false)
{/ 1}}。
因为,当您使用while (!fixed)
时,它不会返回while (fixed = false)
值,而是将boolean
指定为fixed
。您可以看到条件始终为false,循环不会执行。
答案 3 :(得分:0)
第二种情况的代码不正确。您似乎正在尝试进行冒泡排序,但您缺少第二个循环。我确认您当前的代码不对数组进行排序。请尝试使用此代码:
case 2:
int temp;
// Bubble sort the array
for (int i=0; i < array.length; i++) {
for (int j=1; j < (array.length - i); j++) {
if (array[j-1] > array[j]) {
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
for (int j=0; j < array.length; ++j) {
System.out.print(array[j] + "\t");
}
break;
我还删除了while
循环逻辑,因为它似乎没有任何用途。
答案 4 :(得分:0)
while (fixed = false)
fixed = false是一个赋值,它将导致为“false”,因为fixed被赋值为false,这使得while循环永远不会启动。