Java期末考试试卷.pdf

上传人:33****8 文档编号:32081520 上传时间:2022-08-08 格式:PDF 页数:5 大小:15.82KB
返回 下载 相关 举报
Java期末考试试卷.pdf_第1页
第1页 / 共5页
Java期末考试试卷.pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述

《Java期末考试试卷.pdf》由会员分享,可在线阅读,更多相关《Java期末考试试卷.pdf(5页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Java考试一:求两个整数的最大公约数【源代码】import java.util.Scanner; public class text52 public static void main(String args) Scanner sca=new Scanner(System.in); System.out .println(Please enter the first integer: ); int x=sca.nextInt(); System.out .println(Please enter the second integer: ); int y=sca.nextInt(); Syst

2、em.out .println(highest common divisor is +gcd(x,y); public static int gcd(int x,int y) if(xy) int t=x; x=y; y=t; while (x!=0) int temp=y% x; return temp ; 定义一个方法求两个正整数的最大公约数【源代码】import java.util.Scanner; public class demo01 public static void main(String args) Scanner sc=new Scanner(System.in); Sys

3、tem.out.println( 请输入第一个正整数: ); int a=sc.nextInt(); System.out.println( 请输入第二个正整数: ); int b=sc.nextInt(); System.out.println(a+ 和+b+ 的最小公约数为: +getGCD(a,b); / 定义如下方法:方法一: public static int getGCD(int x,int y) / x 接受第一个整数,y 接受第二个整数 int num=1; / 定义一个变量num,来保存最大公约数 for (int i = 1; i =1; a-) / 从 x开始递减遍历 x

4、 到 1 的所有整数if (x%a=0 & y%a=0) / 如果有一个数同时满足被x,y整除,那么这个数就是我们要找的最大公约数return a; / 返回这个最大公约数的值 return 1; / 如果 if 条件不成立,那么返回数值1 【源代码】import java.util.Scanner; publicclass E52 publicstaticvoid main (String args ) Scanner W =new Scanner(System.in ); inta, b, i ; System. out .printf( 请输入两个数字:n); a=W .nextInt(); b=W .nextInt(); if ( ab) for ( i =1; i =a; i +) if ( a% i =0&b% i =0) System.out .printf( %d 是公约数 , i ); else for ( i =1; i =b; i +) if ( a% i =0&b% i =0) System.out .printf( %d 是公约数 , i );

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 工作报告

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com