用java编写升序_编写一个Java应用程序,在键盘上输入一个正整数n,,java程序 输入n个数字,以逗号隔开,然后升序排列,再重新...

导航:网站首页 >

编写一个Java应用程序,在键盘上输入一个正整数n,,java程序 输入n个数字,以逗号隔开,然后升序排列,再重新

编写一个Java应用程序,在键盘上输入一个正整数n,,java程序 输入n个数字,以逗号隔开,然后升序排列,再重新输出搜...

匿名网友:

你要弄懂2个问题1:只有1和它本身两个正因数的自然数,叫质数或称素数; 2:合数都能被 2,3,5,7,11中的一个整除,那么 你可以这样写主逻辑算法:int n ;你见输入的那个数if(n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 11 == 0 ){System.out.print( n + "是个合数");}else{System.out.print( n + "是个素数");}

匿名网友:

import java.util.*;class Tester{public static void main(String args){Scanner sc = new Scanner(System.in);int n =sc.nextInt();sc.close();long sum = 0;for(int i=1;ilong tmp = 1;for(int j=1;jtmp*=j;}sum+=tmp;}System.out.format("%d",sum);}}

匿名网友:

import java.io.BufferedReader;import java.io.InputStreamReader;public class MaoPaoPaiXu { public static void main(String args) throws Exception { System.out.println"请输入要输入的正整数个数:"; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String num = br.readLine(); int len = Integer.parseInt(num); int source = new intlen; System.out.println"请输入正整数,每个正整数之间用\",\"分割:"; String sourceString = br.readLine(); br.close(); String sourceStrs = sourceString.split(","); if (sourceStrs.length != len) { System.out.println"输入的正整数个数和输入的正整数数量不匹配。

"; return; } for (int i = 0; i 0; --i) { for (int j = 0; j < i; ++j) { if (sourcej+1< sourcej) { temp = sourcej; sourcej = sourcej+1; sourcej+1 = temp; } } } System.out.println"冒泡排序后的数组内容:"; StringBuffer sb = new StringBuffer(); for (int string : source) { sb.append(string+","); } System.err.println(sb.toString); }}运行后的内容: 请输入要输入的正整数个数: 6 请输入正整数,每个正整数之间用","分割: 876,234,123,2,32,12 冒泡排序后的数组内容: 2,12,32,123,234,876,

匿名网友:

编程实现计算n!,也就是n*(n-1)*(n-2)*....*1,可以通过递归函数来实现,具体实现方法: #include int Func(int n){if(n int main() {int n,i,s=1;scanf("%d",&n); for(i=1;i<=n;i++)//for循环求累积 s=s*i; printf("%d\n",s); return 0; } /*运行结果:例如求5的阶乘5120*/...

匿名网友:

package com.kk.language_faculty.exception; public class TestThrowsException1 { public static void main(String args) { TestThrowsException1 te = new TestThrowsException1(); te.check(new int { 1, 2, -3, 4, 5, 6, 10, -1, }); } public void check(int arr) { for (int i : arr) { if (i try { throw new NegativeArraySizeException(arr); } catch (NegativeArraySizeException ne) { System.out.println"发生了异常, 正在处理中..." + ne.toString()); } } else { System.out.println("--- " + i + " ---"); } } } } @SuppressWarnings("serial") class NegativeArraySizeException extends Exception { private int array; public NegativeArraySizeException(int array) { this.array = array; } public int getArray() { return array; } }/ package com.kk.language_faculty.exception;/* * 创建一个类Example,有一个String 型参数的方法---check) ,该方法用来检查参数中是否包含英文字母以外的字符。

* 如果包含,则抛出一个MyException异常。

*/ public class TestThrowsException2 { public void check(String str) { char temp = ' '; for (int i = 0; i temp = str.charAt(i); if ((temp >= 'a' && temp = 'A' && temp System.out.println(temp + " 是字母"); } else { try { throw new MyException(str); } catch (MyException e) { System.out.println"异常发生!!!字符串 " + e.getContent) + " 中由字符 " + temp + " 导致异常!" + e.getMessage()); } } } } public static void main(String args) { TestThrowsException2 example = new TestThrowsException2(); example.check("aa00bb11cc88dd//ee..ff,,gg--hh==jj((kk))zz"; } }/* * 创建一个异常类,将其命名为MyException,继承自Exception类。

其中定义一个字符串成员content. * 生成一个构造方法对其赋值。

并有一个成员方法取得其值。

*/ @SuppressWarnings("serial") class MyException extends Exception { private String content; MyException(String content) { this.content = content; } public String getContent() { return content; } }

匿名网友:

public class HelloWorld { public static void main(String args) { System.out.println"请输入一个正整数n"; Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 2; i <= n; i++) // 1不是素数,所以直接从2开始循环 { int j = 2; while (i % j != 0) { j++; // 测试2至i的数字是否能被i整除,如不能就自加 } if (j == i) //当有被整除的数字时,判断它是不是自身,若是,则说明是素数 { System.out.println(i); // 如果是就打印出数字 } } }}...

匿名网友:

import java.util.Scanner;public class Test{ public static void main(String args) { Scanner scan = new Scanner(System.in); int count = 0; int num = scan.nextInt(); for(int i = 1;i<=num;i++){ count +=i; } System.out.println(count); }}...

匿名网友:

package com.chen;import java.util.Scanner;public class jiecheng {static int a; static int e=0; public static void main(String args) { Scanner sc=new Scanner(System.in); a=sc.nextInt(); for(int i=1;i<=a;i++){ int c =Jie(i); e+=c; } System.out.println(e); } public static int Jie(int n){ if(n==1){ return 1; } return n*Jie(n-1); }临时瞎写的 希望能帮上你~...

匿名网友:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Input { public static void main(String args) { String str = null; Integer n = null; Double result = 0d; //read string BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { str = br.readLine(); br.close(); } catch (IOException e) { e.printStackTrace(); } //convert string to integer try{ n = Integer.valueOf(str); } catch (NumberFormatException e) { System.out.println("please input a number!"); } //do your work if(null != n && n!=0){ if(n%2==0){ //even number for(double i=1;i<=n/2;i++){ result += 1/(2*i); } }else{ //odd number for(double i=1;i<=n/2+1;i++){ result += 1/(2*i-1); } } System.out.println("result is:" + result); }else{ System.err.println("please input number correctly!"); } }}

匿名网友:

public static void main(String args) {int a = new int20;Scanner input =new Scanner(System.in);System.out.println"请输入20个数字";for(int i = 0; i ai = input.nextInt();}System.out.println"这二十个数的相加结果为";int jieguo = 0;for(int j = 0;jjieguo +=aj;}System.out.println(jieguo);}

问题推荐

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值