首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

想了一个多小时 越想越越乱 虚心求教解决思路

2012-02-05 
想了一个多小时 越想越越乱 虚心求教从键盘输入3个整数,分别赋给整型变量a.b.c,然后将输入的整数按照从小

想了一个多小时 越想越越乱 虚心求教
从键盘输入3个整数,分别赋给整型变量a.b.c,然后将输入的整数按照从小到大的顺序放在变量a,b,c中,并输入三个变量的值。

ps(我想a与b,a与c进行比较,然后b与c 比较 我不知道怎么用码出来.....)

老师没教 直接跳过....纠结... 目前只会用if else 做不出来。

只得的到最小值或最大值....


import java.util.Scanner;
public class foree {

public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个整数");
int a = input.nextInt();
System.out.print("请输入第二个整数");
int b = input.nextInt();
System.out.print("请输入第三个整数");
int c = input.nextInt();
if (a>b){
System.out.print("a=" + b );
}
else if(a<b){
System.out.print("a=" + a );
}
else if(){

}
}


[解决办法]
用简单的冒泡排序实现的从小到大排序,你参考下

Java code
import java.util.Scanner;public class Foree {    public static  void rangeAry(int[] ary){          for(int i=0;i<ary.length;i++){                for(int j=0;j<ary.length-1;j++){                      if(ary[j]>ary[j+1]){                          int temp=ary[j];                          ary[j]=ary[j+1];                          ary[j+1]=temp;                      }                }          }    }        public static void main (String[] args){        int[] ary=new int[3];        Scanner input = new Scanner(System.in);        System.out.print("请输入第一个整数");        ary[0] = input.nextInt();        System.out.print("请输入第二个整数");        ary[1] = input.nextInt();        System.out.print("请输入第三个整数");        ary[2] = input.nextInt();        rangeAry(ary);        for(int i:ary){            System.out.print(i+",");        }    }}
[解决办法]
给你个思路吧 
排序法
int[] m = {2,10,4,7,5,8,6,9,1,3};
for(int i = 0;i < m.length - 1;i++) //排序次数
{
//两两比较,实现排序
for(int j = 0;j < m.length - 1 - i;j++)
 {
if(m[j] < m[j + 1])
{
//交换
int temp = m[j];
m[j] = m[j + 1];
m[j + 1] = temp;
}
 }
}
[解决办法]
Java code
import java.util.*;class foree {        public static void main (String[] args)        {            Scanner input =new Scanner(System.in);                    System.out.print("请输入第一个整数");            int a = input.nextInt();            System.out.print("请输入第二个整数");            int b = input.nextInt();            System.out.print("请输入第三个整数");            int c = input.nextInt();            int temp;            if(a>b)            {                temp=a;                a=b;                b=temp;            }            if(a>c)            {                temp=a;                a=c;                c=temp;            }            if(b>c)            {                temp=b;                b=c;                c=temp;            }         System.out.println(a+","+b+","+c);        }} 

热点排行