导航:首页 > 编程语言 > java编程题

java编程题

发布时间:2022-01-24 21:50:32

‘壹’ 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编程题

只需要创建两个文件就好了,第一个Sex枚举,第二个,录入student类。
代码如下:

/**
* @author brave
*
* @2020-2-23 下午1:41:31
*/
public enum Sex {
MAN("男",0),WOMEN("女",1),OTHER("其他",2);

private String name;
private int index;
Sex(String name,int index){
this.name=name;
this.index=index;
}

public static String getName(int index){
for (Sex sex:Sex.values()) {
if(sex.getIndex() == index){
return sex.name;
}
}
return "";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}

}
主要定义性别有几种类型。
以下是实现类:
/**
*
*/
package com.student;

import java.util.Scanner;

/**
* @author Brave
*
* 2020-2-23 下午3:58:07
*/
public class StudentTwo {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

String name[] = new String [3];
String number[] = new String[3];
String sex[] = new String[3];
for (int i = 0; i < 3; i++) {
int num = i+1;
System.out.println("请输入第" + num + "个学生的学号:");
number[i] = input.next();
System.out.println("请输入第"+ num + "个学生的姓名:");
name[i] = input.next();
System.out.println("请输入第" + num + "个学生的性别(男请输入0,女请输入1,其他请输入2):");
sex[i] = input.next();
}

System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
System.out.println("-----------------以下是录入的学生信息----------------");

for (int i = 0; i < name.length; i++) {
System.out.println("第一个学生的姓名是:"+name[i] +",学号:"+number[i]+",性别:"+Sex.getName(Integer.parseInt(sex[i])));
}
}
}

‘叁’ java题目编程题

首先初始化一个HashMap,存入一些用户名和电话号码的初始值,然后分别使用map的API来进行相应的map增删改查操作就行了。

增/改:put(K key,V value),这里可以初始化用户名为key,电话号码为value,前提是用户名要没有重复的,如果是相同的用户名会覆盖前一个,可以当做改功能来使用
删:remove(Object key)根据键,删值,如果之前的key存的用户名,那么这里就是根据用户名删值
查:get(Object key)根据键查值,相当于根据用户名查电话号码

‘肆’ 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编程题

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编程题

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;//返回结果
}
}

‘柒’ 8道简单的Java编程题

第一题
private static void tuzinum(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
//总数
int allnum = 1;
//成年数
int chengniannum = 0;
//出生数
int chushengnum = 0;
for(int i=0;i<12;i++){
for(int j=0;j<list.size();j++){
Integer age = list.get(j);
age++;
list.set(j, age);
if(age>3){
chengniannum++;
chushengnum++;
allnum++;
list.add(1);
}
}
System.out.println("第"+i+"个月兔子对数为:"+list.size());
}
}

第二题
private static void fenjie(){
int num = 90;

boolean canchu = true;
while(canchu){
int time = 0;
for(int i=2;i<num/2;i++){
if(num%i==0){
num = num/i;
System.out.println(i);

time++;
}
}
if(time == 0 ){
canchu = false;
System.out.println(num);
}
}
}

‘捌’ 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));
}
}

阅读全文

与java编程题相关的资料

热点内容
哪个app充值可以用银联二维码 浏览:563
女程序员和孩子玩 浏览:837
程序员苏州武汉 浏览:754
大脚插件如何切换安卓 浏览:941
python课设制作年历 浏览:405
明文在pdf 浏览:750
郑永令pdf 浏览:122
cad命令行坐标输入 浏览:781
编译原理csdn博客 浏览:194
想在深圳买房关注哪个app 浏览:913
国际体验服为什么服务器加载失败 浏览:690
php接口用处 浏览:394
想推广app去哪里找 浏览:258
phpcmysql 浏览:123
安卓手机的百度carlife怎么连接车 浏览:138
编译安装py 浏览:162
pythonchar转int 浏览:220
基于单片机的多组密码锁设计论文 浏览:108
华为云如何升级云服务器 浏览:213
生活垃圾压缩直运站 浏览:751