中易网

java 字符比较

答案:2  悬赏:20  
解决时间 2021-05-24 16:50

有个排序程序如下(错误地方我已将它标注):

import java.util.*;

public class Sort
{
public static void BubbleSort(int a[])
{
int i,j,temp;
for(i=0;i<a.length;i++)
for(j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println("the sorted num is:");
for(i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}

public static void InsertSort(int a[])
{
int i,inserter,index;
for(i=1;i<a.length;i++)
{
inserter=a[i];
index=i-1;
while(index>=0&&inserter<a[index])
{
a[index+1]=a[index];
index--;
}
a[index+1]=inserter;
}
System.out.println("the sorted num is:");
for(i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}

public static void main(String [] args)
{
int n,m;
String str;
Scanner input=new Scanner(System.in);
System.out.print("Please input how many data the array have:");
n=input.nextInt();
int a[]=new int[n];
System.out.print("input "+n+" elements:");
for(int i=0;i<n;i++)
{
a[i]=input.nextInt();
}
System.out.println("\t\tstart to sort number");
System.out.println("*********************************************");
System.out.println("\t\t 1.Bubble Sort");
System.out.println("\t\t 2.Insert Sort");
System.out.println("*********************************************");
System.out.print("chose your method to sort number:");
m=input.nextInt();
do{
switch(m)
{
case 1: BubbleSort(a); break;
case 2: InsertSort(a); break;
default: System.out.print("your choose is ,please chose again!");break;
}
}while(m!=1&&m!=2);
System.out.println("\nwhether you want to continue to sort num or not,if you want to continue ,please input Y,else input N.");
str=input.next();
while(str=="Y") //无法比较
{
System.out.print("chose your method to sort number:");
m=input.nextInt();
switch(m)
{
case 1: BubbleSort(a); break;
case 2: InsertSort(a); break;
default: System.out.print("your choose is ,please chose again!");break;
}
str=input.next();
}
}
}

为什么不能进行字符比较,可不可以输入字符char 型数据(如果可以就写下来一下)。因为刚学程序,算法不是很好,希望高手帮我改哈,不甚感谢。

最佳答案

字符比较是用equals 比较 而不是用==

全部回答
改成while("Y".equals("str"))程序会更健壮一点
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
dnf萝卜G现在不好使了吗?是不是没了?
洪山区如家快捷酒店(武汉光谷步行街店) 介绍
怎么让QQ空间名称为空
购买印刷用的铅字(急)
怎么样可以快速的减脂肪
分手之后的情侣为什么不能做朋友?
读卫校好还是读幼师好?
谁有小孩幼的片和 神秘男孩或风的影片共享下
买个3000左右的电脑,玩游戏的什么配置好啊...
今年的NBA大蓬车有武汉站吗?
汉阳区香港现代青少儿英语(汉阳人信汇校区)这
谁有QQ宝贝小助手
上墙我想知道这个在什么地方
飞机和爱情有什么关系?
怎样计算解除一个徒弟扣多少名师点
推荐资讯
初中秋季运动会稿件200左右 告急!
柘城县旋转8角小火锅我想知道这个在什么地方
我想续年会员
谁有音信种子和QQ爱墙种子啊 给我一个 谢谢
洪山区九坤假日酒店(仁和路)在哪里啊,我有事
谁能免费给我能一首歌
很无辜、很难过、我应该怎么办、
已知:如图,CD垂直AB于D,BE垂直AC于E,角1=
宛城区贝贝家双语幼儿园地址在哪,我要去那里
谁给个QQ空间小窝模式网站
夏季吃什么东西降火
为什么我问问的积分和经验会下降呢
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?