初学者java小程序,求java大神帮我补充一下空缺代码!打印出两个整数a和b的最大公约数。

初学者java小程序,求java大神帮我补充一下空缺代码!打印出两个整数a和b的最大公约数。,第1张

a  = scnextInt();

b  = scnextInt();

a < 0||b < 0

a

完整代码:

public class ComDiv {

public static void main(String[] args) {

int a;

int b;

int r;

Scanner sc = new Scanner(Systemin);

do {

Systemoutprintln("请输入两个整数:");

a  = scnextInt();

b  = scnextInt();

} while (a < 0||b < 0);

scclose();

while(b != 0){

r = a%b;

a = b;

b = r;

}

Systemoutprintln("两个数的最大公约数是:" +a);

}

}

演示:

import javaawt;

import javaawtevent;

import javaapplet;

import javaio;

public class testframe extends Frame implements ActionListener

{

Button b1;

Panel p1;

Label text1;

TextField text2;

TextArea text3;

int num[];

int i;

String str;

int times=0;

int A,B;

public testframe(String str)

{

b1=new Button("确定");

p1=new Panel();

text1=new Label("输入四位整数:");

text2=new TextField(10);

text3=new TextArea(10,10);

p1setLayout(null);

p1setBackground(ColorGRAY);

setSize(300,300);

setVisible(true);

b1setBackground(ColorBLUE);

b1setForeground(ColorWHITE);

b1setSize(80,30);

p1add(text1);

p1add(text2);

p1add(text3);

p1add(b1);

text1setBounds(0,0,100,20);

text2setBounds(0,2text1getBounds()height,150,20);

text3setBounds(0,3text1getBounds()height,150,150);

b1setBounds(200,50,60,30);

add(p1);

b1addActionListener(this);

addWindowListener(new shut());

}

public void rand()

{ num=new int[10];

for(i=0;i<4;i++)

{

num[i]=(int)(Mathrandom()10);

int j=0;

while(j<i)

{if(num[j]==num[i])

{num[i]=(int)(Mathrandom()10);

continue;

}

j++;

}

}

}

public void compair()

{

int x,m,n;

x=IntegerparseInt(text2getText());

A=0;B=0;

for(m=3;m>-1;m--){

for(n=0;n<4;n++){

if((int)(x/Mathpow(10,m))==num[n])

{

if(m+n==3)

A++;

else

B++;

}

}

x=x-(int)(x/Mathpow(10,m))(int)(Mathpow(10,m));

}

}

public void actionPerformed(ActionEvent e)

{

if(egetSource()==b1)

if(times<5){

if(IntegerparseInt(text2getText())<1000||IntegerparseInt(text2getText())>9999)

text2setText("输入数字不合法");

else

{ if(A==4)

text2setText("猜对了");

times++;

compair();

text3append("第");

text3append(StringvalueOf(times));

text3append("猜的结果是:");

text3append(StringvalueOf(A));text3append("A");

text3append(StringvalueOf(B));text3append("B");

text3append("\n");

}

}

else

{

text3setText("超过5次,重新开始\n");

times=0;

rand();

}

}

public static void main(String[] args)

{

new testframe("窗口程序");

}

}

class shut extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

Systemexit(0);

}

}

public class HelloWorld{

public static void main(String[] args){

Systemoutprintln("hello world!");

}

}

一 基本概念

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

二 体系

Java分为三个体系,分别为Java SE(J2SE,Java2 Platform Standard Edition,标准版),

JavaEE(J2EE,Java 2 Platform, Enterprise Edition,企业版)。

Java ME(J2ME,Java 2 Platform Micro Edition,微型版)。

package comdemo;

import javaawtBorderLayout;

import javaawtDimension;

import javaawteventActionEvent;

import javaawteventActionListener;

import javautilTimer;

import javautilTimerTask;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJTextField;

public class TimerTest {

private Timer timer;

private JTextField field;

private JButton button;

private boolean flag = true;

public TimerTest() {

timer = new Timer();

addview();

}

private void addview() {

JFrame frame = new JFrame("Timer test");

field = new JTextField();

fieldsetPreferredSize(new Dimension(0, 30));

button = new JButton("start");

buttonsetPreferredSize(new Dimension(100, 30));

buttonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (flag) {

auto();

flag = false;

buttonsetText("stop");

} else {

timercancel();

flag = true;

buttonsetText("start");

}

}

});

frameadd(field, BorderLayoutCENTER);

frameadd(button, BorderLayoutEAST);

framesetBounds(200, 200, 300, 60);

framesetVisible(true);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

public static void main(String[] args) {

new TimerTest();

}

private void auto() {

timerschedule(new TimerTask() {

@Override

public void run() {

int num = IntegerparseInt(fieldgetText()trim());

num += 1;

fieldsetText(num + "");

auto();

}

}, 1000);

}

}

//第一题:

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

import javautilArrayList;

import javautilList;

import javautilArrayList;

import javautilList;

public class ManageName {

List<String> namelist = new ArrayList<String>();

public void PrintMenu() {

Systemoutprintln("\n\r1Add new name " + "2Display all name"

+ " 3Quit"

+ "\nPlease select menu(1,2,3)");

}

public void displayname() {

for (int w = 0; w < thisnamelistsize(); w++) {

Systemoutprintln("Name" + w+":" + thisnamelistget(w));

}

}

public List addName(String name) {

namelistadd(name);

return namelist;

}

public static void main(String[] args) throws IOException {

ManageName a = new ManageName();

aaddName("jing5083394");

BufferedReader in = new BufferedReader(new InputStreamReader(Systemin));

while (true) {

aPrintMenu();

String content = inreadLine();

if (contentequalsIgnoreCase("3")) {

break;

} else if (contentequalsIgnoreCase("1")) {

String name;

BufferedReader in2 = new BufferedReader(new InputStreamReader(

Systemin));

Systemoutprintln("Please input the Name to add new user");

String content2 = inreadLine();

name = content2;

aaddName(name);

Systemoutprintln("Add name successfully\r\n");

}

else if (contentequalsIgnoreCase("2")) {

adisplayname();

}

else {

Systemoutprintln("Invalid selections!\n");

}

}

}

}

//第二题: 把文件放到namefiletxt

/

从namefiletxt文件中读入的字符串全部转换成大写字母,

再按原来的顺序输出到yoursDatatxt文件中。

/

import javalangCharacter;

import javaio;

//将一个文件复制到另一个文件中(覆盖)

public class Filestream {

public static void main(String args[]) {

try {

File inFile = new File("c:\\namefiletxt");

File outFile = new File("c:\\yoursDatatxt");

FileInputStream fis = new FileInputStream(inFile); // 读输入文件

FileOutputStream fos = new FileOutputStream(outFile);

int c;

while ((c = fisread()) != -1) {

c = CharactertoUpperCase(c);

foswrite(c);

}// 写入文件中

Systemoutprintln("Output file finish");

fisclose();

fosclose();

} catch (FileNotFoundException e) {

Systemoutprintln("FileStreamsTest: " + e);

} catch (IOException e) {

Systemerrprintln("FileStreamsTest: " + e);

}

}

}

以上就是关于初学者java小程序,求java大神帮我补充一下空缺代码!打印出两个整数a和b的最大公约数。全部的内容,包括:初学者java小程序,求java大神帮我补充一下空缺代码!打印出两个整数a和b的最大公约数。、求用java语言制作猜物品价格的小游戏的程序代码、一段最简单的java代码程序有哪些等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9993184.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存