【java – 这个while循环如何知道何时失败?】教程文章相关的互联网学习教程文章

java – 使用while循环为用户输入多个int并找到max和min【代码】

所以我的作业问题是提示用户一系列整数,并找到这些整数的最大值和最小值.使用循环和-99来打破循环.下面是我的代码,但我的问题是,有更短的方法吗?随时发表评论,感谢您阅读本文.Scanner input=new Scanner(System.in);int num1,num2,max,min;System.out.print("enter a number: ");num1=input.nextInt();System.out.print("enter another number: ");num2=input.nextInt();max=Math.max(num1,num2);min=Math.min(num1,num2);while (...

Java基础 while 简单示例

??? JDK :OpenJDK-11 ??? OS :CentOS 7.6.1810 ??? IDE :Eclipse 2019?03 typesetting :Markdowncode package per.jizuiku.base;/*** @author 给最苦* @date 2019/06/29* @blog www.cnblogs.com/jizuiku*/ class Demo {/*** @param args*/public static void main(String[] args) {int i = 0;while (i < 3) {System.out.println("hello");i++;}System.out.println("---------------");for (int j = 0; j < 3; j++) {System.o...

java – ArrayList While Loop给出IndexOutOfBoundsException【代码】

我正在尝试编写一种方法,使用锦标赛风格比较来确定数组列表的最大值.但是,我想我不了解while循环,因为我无法获得所需的输出,而是得到一个IndexOutOfBoundsException. 这是我的代码:import java.util.*; public class TournamentMax {public static <T extends Comparable<? super T>> ArrayList<T> tournament(ArrayList<T> tournamentArrayList) {ArrayList<T> winners = new ArrayList<T>();int n = tournamentArrayList.size()...

java – While内部赋值循环导致checkstyle错误【代码】

我正在浏览我的代码以确保它符合CheckStyle标准. 我个人认为规则“无内部任务”使代码更难理解(你需要查看3个地方而不是1个). 有没有办法通过在while循环中创建一个{}块来保存我的单个区域来执行我的赋值并返回一个布尔值?! 你有什么看法?File file = new File("C:\\test.txt"); FileInputStream fileInputStream = new FileInputStream(new FileInputStream(file));// Inner Assignment while ((int i = fileInputStream.readL...

根据输入的数量在while循环中创建多个java线程【代码】

我正在尝试编写一个java程序,它从数据库中读取信息,并为表的每一行创建一个新线程.所以我不知道我需要多少线程.到目前为止我有这个:con = DriverManager.getConnection(url, user, passwd);pst = con.prepareStatement("select hostname, ipadress, vncpassword from infoscreens");rs = pst.executeQuery();int i=0;while (rs.next()) {i++;Thread tread[i] = new Savescreenshots(rs.getString(1),rs.getString(3),rs.getStrin...

java – 这个while循环如何知道何时失败?【代码】

我正在阅读有关如何编程网络套接字并运行这段代码的内容:try { while (true) { // This is the line in questionint i = in.read( );if (i == -1) break;System.out.write(i);}}catch (SocketException e) {// output thread closed the socket}catch (IOException e) {System.err.println(e);}第二行如何知道何时失败?换句话说,while(true)循环如何工作?我想我不明白’虽然这是真的吗?’解决方法:这里的重要一点是:if...

java – While循环使用嵌套的if / else语句【代码】

嗨,我无法让我的程序正常运行.我能够清除任何语法错误,但现在我已经发出了我的输出. 首先,在第一个IF语句中,它会提示该人同时输入他们的姓名和部门,这样当输出时,名称为空,只有部门输入.我认为它与整个IF语句有关,因为如果我将“String name”更改为input.next,则名称提示正确,但dept和totalHrsWkd合并在一起. 此外,在测试我的程序时,当我输入totalHrsWkd的负数时,它会崩溃.它将在一行中显示两个打印语句,然后崩溃JCreator. 我对此...

java – 为什么这个while循环工作?【代码】

为什么这样做:Scanner keyboard = new Scanner(System.in); int i; int beerBottles = 100;//Asks the user for the number of beer bottles on the wall System.out.println("How many bottles of beer are on the wall?");while (!keyboard.hasNextInt()) { System.out.println("Make sure you enter an integer."); keyboard.next(); } //Sets beerBottles to the user entered value beerBottles = keyboard.nextInt();我偶然...

Java Do While Statement有两个条件【代码】

我正在尝试学习java,但我一直试图做一个关于Do While Statement的单个程序,有两个条件.具体来说,我想要一个方法运行,直到用户写“是”或“否”.那么,有我的东西,它有什么问题?String answerString;Scanner user_input = new Scanner(System.in);System.out.println("Do you want a cookie? ");do{answerString = user_input.next();if(answerString.equalsIgnoreCase("yes")){System.out.println("You want a cookie.");}else if(...

java – 使用next方法时,链接列表不会正确迭代while循环【代码】

我试图使用堆栈迭代地反转链表.我已经确定了问题发生的地方,但是对于我的生活,我无法弄清楚当我调用ListNode的下一个方法时代码没有正确迭代的原因.我在下面的代码中标记了bug发生的地方. 这是我运行代码时的结果:Before: 1->2->3->4->null After: 4->3->3->4->null这是结果应该是:Before: 1->2->3->4->null After: 4->3->2->1->null谁能指出我正确的方向是什么?谢谢! 这是代码:public class Solution {public static void ma...

while循环和队列的奇怪java行为【代码】

我有while(true)循环的奇怪行为.这是代码: 作为班级成员,我有:static Queue<Object> response = new LinkedList<Object>();……和一个功能:private void read() {while (true){System.out.println("foo");if(!(response.isEmpty())){if((Boolean)response.peek() == true){view.dispose();LogInControler controler= new LogInControler();disableMasterLogin();response.poll();return;}else if((Boolean)response.poll() == f...

java – 在使用空的while循环初始化片段之前等待导航抽屉关闭【代码】

根据DrawerLayout文档,“避免在动画期间执行昂贵的操作,例如布局,因为它可能导致卡顿”. 因此,我试着在继续之前等待抽屉关闭:@Override public void onItemClick(AdapterView parent, View view, int position, long id) {// Close the drawermDrawerLayout.closeDrawer(mDrawerList);ExecutorService es = Executors.newSingleThreadExecutor();final int mPosition = position;Thread navThread = new Thread(new Runnable() {@...

Java中的do-while循环——通过示例学习Java编程(11)【图】

作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了while循环。在本教程中,我们将讨论java中的do-while循环。do-while循环类似于while循环,但是它们之间有一个区别:在while循环中,循环条件在循环的主体执行之前进行评估,而在do-while循环中,循环条件在循环的主体执行之后再进行评估。 do-while循环的语法:1 2 3 4do { ???statement(s); }?while(conditi...

学JAVA第六天,运算符、表达式、if语句以及for、while、都循环

今天老师讲的内容有点多,但是都是在学C#时学过的,用法都差不多,所以很好理解。 算术运算符:+, - ,* , / ,% ,++ ,-- 关系运算符:>,<,>=,<=,==,!=。意思是:大于,小于,大于或等于,小于或等于,等于和不等于 逻辑运算符:&&,||,!。与,或,非 赋值运算符:+=,-=,*=,/=。例如:i+=3;//等效于i=i+3; 条件运算符:? : 例如: Int A,B,C; A=2; B=3; C=A>B ? 100 :200; 这条语句的意思是,如果A>B的话,就将100赋给C,否...

JAVA语法——使用while循环语句与自增运算符循环遍历数组【代码】

package 使用while循环语句与自增运算符循环遍历数组;public class 使用while循环语句与自增运算符循环遍历数组 {public static void main(String[] args) {// TODO Auto-generated method stubString []aves = new String[] {"白鹭","黄莺","鹦鹉","乌鸦","喜鹊","布谷鸟","斑鸠","百灵鸟"};int index = 0;System.out.println("我的花园里有很多鸟,大约包括");while(index < aves.length)System.out.println(aves[index++]);}}