當前位置:首頁 » 編程語言 » java編程題

java編程題

發布時間: 2022-01-08 06:41:22

java編程題目

重點是根據a的值,生成各個數,先根據數字重復長度生成對應的字元串,再利用循環進行相加,相加時把字元串轉換成數字即可。

㈡ Java編程題

先把題目列成公式就是2*a+3*b+4*c+5*d=20

這就是一個四元一次方程
簡單點就四次循環就可以得到答案
public static void main(String[] args) {

int min=0;//不可能買負數
for (int a =min; a <=20/2; a++) {
for (int b =min; b <20/3; b++) {
for (int c = min; c <20/4; c++) {
for (int d = min; d <20/5; d++) {
if(2*a+3*b+4*c+5*d==20){
System.out.println("菊花"+a);
System.out.println("康乃馨"+b);
System.out.println("玫瑰"+c);
System.out.println("百合"+d);
}
}

}
}
}

}

㈢ java題目編程題

首先初始化一個HashMap,存入一些用戶名和電話號碼的初始值,然後分別使用map的API來進行相應的map增刪改查操作就行了。

增/改:put(K key,V value),這里可以初始化用戶名為key,電話號碼為value,前提是用戶名要沒有重復的,如果是相同的用戶名會覆蓋前一個,可以當做改功能來使用
刪:remove(Object key)根據鍵,刪值,如果之前的key存的用戶名,那麼這里就是根據用戶名刪值
查:get(Object key)根據鍵查值,相當於根據用戶名查電話號碼

㈣ java編程題

使用Random類和for循環輸出0~100內的隨機整數


importjava.util.Random;
publicclassRandomPrint{
publicstaticvoidmain(String[]args){
Randomr=newRandom();//不傳入種子
for(inti=0;i<8;i++){
System.out.println(r.nextInt(100));
}

}
}

㈤ java編程十道題

1.
public class TestRandomArray
{
public static void main(String[] args)
{
int maxline = (int) (Math.random()*100);
int[] array = new int[maxline];
for(int i=0;i<maxline;i++)
{
int temp = (int) (Math.random()*100);
if(temp>=10&&temp<=99)
{
array[i] = temp;
}
else
i--;
}
for(int j=0;j<maxline;j++)
{
int max = array[0];

if(array[j]>max)
{
max=array[j];
array[j] = array[0];
array[0] = max;

}
}
for(int k=0;k<maxline;k++)
{
System.out.print(array[k]+" ");
}
}
}

2.
public class TestNumber100
{
public static void main(String[] args)
{
for(int i=1;i<100;i++)
{
if(i%3==0&&(i%5!=0)&&(i%9!=0))
System.out.println(i+" ");
else
continue;
}
}
}

3.貌似沒結果
public class Test3
{
public static void main(String[] args)
{
/*for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
int code = 10000+i*1000+j*100+23;
if((code%57)==0&&(code%67)==0&&i!=j)
{
System.out.println("x="+i+","+"y="+j);
}
else
System.out.println(i+" "+j);
}
}*/

for(int i=10023;i<=19923;i++)
if(i==0&&i==0)
System.out.println(i+" ");
}
}

4.
class Caculate {
int money;
Caculate(){
money = 10;
}
public int charge (int distance){
if (distance < 3){
money = 10;
}
if ((distance >= 3) && distance < 15)
{
money = money + 2*(distance-3);
}
if (distance > 15 )
{
money = money + 2*12 + 3*(distance - 15);
}

return money;
}
public static void main(String args[]) {

System.out.println("請輸入公里數");
int distance_example=0;
Scanner key = new Scanner(System.in);
int n = key.nextInt();
distance_example = n;
Caculate pay = new Caculate();
pay.money = pay.charge(distance_example);
System.out.println("You need to pay money:"+pay.money+" distance is:"+distance_example);

}
}

5.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

6.
public class TestMoney
{
public static void main(String[] args)
{
int one=1,two=2,five=5;
for(int i=1;i<100;i++)
for(int j=1;j<50;j++)
for(int k=1;k<20;k++)
{
if((i*one+j*two+k*five)==100)
System.out.println(i+"個一分,"+j+"個兩分,"+k+"個五分");
}

}

}

7.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestJieCheng
{
public static void main (String args[])
{
int a=0,b=1;float sum=0;
System.out.print("請輸入要計算的階乘:");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(in.readLine());
}catch(IOException e){}
for(int i=1;i<=a;i++)
{
b=b*i;

}
System.out.print(a+"的階乘是"+b);
}
}

8.
public class Test4
{

public static void main (String args[])
{
int n=100;
while(n<=1000)
{
int i,j,k;
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i, 3)+Math.pow(j, 3)+Math.pow(k, 3))==n)

System.out.print(n+String.valueOf('\t'));
n++;
}
}
}

9.
public class TestNumber2
{
public static void main(String[] args)
{
final int maxline = 20;
double sum = 0;
double [] denominator = new double [maxline];
double [] numerator = new double [maxline];
for(int i=2;i<denominator.length;i++)
{
denominator[0]=1;
denominator[1]=2;
denominator[i]=denominator[i-1]+denominator[i-2];
}
for(int j=2;j<numerator.length;j++)
{
numerator[0]=2;
numerator[1]=3;
numerator[j]=numerator[j-1]+numerator[j-2];
}
for(int k=0;k<maxline;k++)
{
sum+=(numerator[k])/denominator[k];

}
System.out.println(sum);
}

}

10.
import java.io.*;

public class 根據輸入的三個數求一元二次方程
{
public static void main (String args[]) throws NumberFormatException, IOException
{
float a[]=new float [3];
for(int i=0;i<a.length;i++)
{
System.out.println("請輸入第"+(i+1)+"個數");
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
a[i]=Float.parseFloat(in.readLine());
}
float x1=0,x2=0;
x1=(float) ((((-1)*a[1])+(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
x2=(float) ((((-1)*a[1])-(Math.sqrt(a[1]*a[1]-4*a[0]*a[2])))/(2*a[0]));
System.out.print(a[0]+"X2"+a[1]+"X"+a[2]+"的兩個根是:");
System.out.print(x1+"和"+x2);
}
}

㈥ JAVA編程題

publicclassTest{
publicstaticvoidmain(String[]args){
int[]arr={12,22};//指定數組
intresutl=getSum(arr);//求十位數為奇數的數值的和
System.out.println(resutl);
}

publicstaticintgetSum(int[]arr){
intresult=0;//結果
for(inti=0;i<arr.length;i++){//注意這里的循環次數是arr的長度,而不是100
intnum=arr[i];//從數組里取出這個數字
if(num>9&&num<100){//題目要求數字是1~100之間的數,且有十位,那麼說明數值范圍是[10,99]
if((num/10)%2!=0){//如果十位是奇數.
result+=num;//那麼累加到結果里
}
}
}
returnresult;//返回結果
}
}

㈦ java編程題

importjava.util.Arrays;
importjava.util.Scanner;
publicclassTestExample{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
intn=Integer.parseInt(scanner.nextLine());
int[][]index=newint[n][2];//數組存儲相關信息
for(inti=0;i<n;i++){
String[]a=scanner.nextLine().split("\s");
intx=Integer.parseInt(scanner.nextLine());
index[i][0]=x;
index[i][1]=-1;
for(intj=0;j<6;j++){
if(String.valueOf(x).equals(a[j])){
index[i][1]=j;
break;
}
}
}
//數組信息輸出
for(inti=0;i<n;i++){
if(index[i][1]!=-1){
System.out.println(index[i][0]+":"+"a["+index[i][1]+"]");
}else
System.out.println(index[i][0]+":"+"notfound");
}
scanner.close();
}
}

㈧ Java編程題

package joshua.fraction.test;
public class Fraction extends Object {
private int numerator;
private int denominator;
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public String addition(Fraction fraction){
int numerator;
int denominator;
int greatestCommonDivisor;
StringBuilder result = new StringBuilder();
result.append("(");
if(this.denominator==fraction.getDenominator()){
numerator = this.numerator + fraction.getNumerator();
denominator = this.denominator;
}else{
numerator = this.numerator*fraction.getDenominator()+fraction.getNumerator()*this.denominator;
denominator = this.denominator*fraction.getDenominator();
}
greatestCommonDivisor = getGreatestCommonDivisor(numerator, denominator);
return result.append(numerator/greatestCommonDivisor).append(")/(").append(denominator/greatestCommonDivisor).append(")").toString();
}
public String subtraction(Fraction fraction){
int numerator;
int denominator;
int greatestCommonDivisor;
StringBuilder result = new StringBuilder();
result.append("(");
if(this.denominator==fraction.getDenominator()){
numerator = this.numerator - fraction.getNumerator();
denominator = this.denominator;
}else{
numerator = this.numerator*fraction.getDenominator()-fraction.getNumerator()*this.denominator;
denominator = this.denominator*fraction.getDenominator();
}
greatestCommonDivisor = getGreatestCommonDivisor(numerator, denominator);
return result.append(numerator/greatestCommonDivisor).append(")/(").append(denominator/greatestCommonDivisor).append(")").toString();
}
@Override
public String toString() {
return "(" + numerator + ")/( " + denominator + ")";
}
@SuppressWarnings("unused")
private int getLeastCommonMultiple(int a, int b){
int sum = a*b;
return sum/getGreatestCommonDivisor(a, b);
}
private int getGreatestCommonDivisor(int a, int b){
int r;
for (;;) {
r = a % b;
a = b;
b = r;
if (r == 0) {
break;
}
}
return a;
}
}

package joshua.fraction.test;
public class Test {
public static void main(String[] args) {
Fraction a = new Fraction(1, 3);
Fraction b = new Fraction(1, 2);
System.out.println("" + a + "+" + b + "=" + a.addition(b));
System.out.println("" + a + "-" + b + "=" + a.subtraction(b));
}
}

第二題:

package joshua.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test {
public static void main(String[] arg) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input a Integer:");
StringBuffer sb = new StringBuffer();
Iterator<Integer> it = null;
try {
String input = reader.readLine();
it = find(Integer.valueOf((input == null || input.length() == 0) ? "0" : input)).iterator();
while (it != null && it.hasNext()) {
sb.append(it.next()).append(",");
}
if(sb.toString().length()>0)
System.out.println("result is: "+ sb.toString().substring(0, sb.toString().length() - 1));
reader.readLine();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<Integer> find(int arg){
List<Integer> list = new ArrayList<Integer>();
for(int i=1; i<arg; i++){
if(i%7==0){
list.add(i);
}else{
String str = String.valueOf(i);
char[] cs = str.toCharArray();
for(int j=0; j<cs.length; j++){
if("7".equals(String.valueOf(cs[j]))){
list.add(i);
break;
}
}
}
}
return list;
}
}

㈨ Java-編程題

public class Main {

public static int reverseInt(int input) {
int reversedNum = 0;
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
return reversedNum;
}

public static void main(String[] args) {
int input = 352;
int reversed = reverseInt(input);
System.out.println("Reversed=" + String.valueOf(reversed));
}
}

熱點內容
安卓上哪裡下大型游戲 發布:2024-12-23 15:10:58 瀏覽:186
明日之後目前適用於什麼配置 發布:2024-12-23 14:56:09 瀏覽:51
php全形半形 發布:2024-12-23 14:55:17 瀏覽:826
手機上傳助手 發布:2024-12-23 14:55:14 瀏覽:730
什麼樣的主機配置吃雞開全效 發布:2024-12-23 14:55:13 瀏覽:828
安卓我的世界114版本有什麼 發布:2024-12-23 14:42:17 瀏覽:708
vbox源碼 發布:2024-12-23 14:41:32 瀏覽:275
詩經是怎麼存儲 發布:2024-12-23 14:41:29 瀏覽:657
屏蔽視頻廣告腳本 發布:2024-12-23 14:41:24 瀏覽:417
php解析pdf 發布:2024-12-23 14:40:01 瀏覽:816