我是 JAVA 新手(顺便说一下计算机编程)。以下程序检查输入是否为二进制。它应该提示用户重新输入整数,直到输入二进制数。但是这个程序做的恰恰相反。
如果输入是二进制的,它要求我重新输入整数,并且当输入非二进制时程序终止。
我需要认真的帮助。这是我的输出
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | int value, userValue; int binaryDigit = 0, notBinaryDigit = 0; Scanner scan = new Scanner(System.in); while (true) { System.out.println("Please enter positive integers:"); userValue = scan.nextInt(); value = userValue; while (userValue > 0) { if ((userValue % 10 == 0) || (userValue % 10 == 1)) { binaryDigit++; } else { notBinaryDigit++; } userValue = userValue / 10; } if (notBinaryDigit > 0) { System.out.println(value +" is a not a Binary Number."); break; } else { System.out.println(value +" is a Binary Number."); } } } |
答案可能为时已晚。但是如果使用该方法,代码可以大大简化。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class MainClass { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (true) { System.out.println("Please enter positive integers:"); int userValue = scan.nextInt(); if (isBinary(userValue)) { System.out.println(userValue +" is a Binary Number."); break; } else { System.out.println(userValue +" is a not Binary Number."); } } } public static boolean isBinary(int input) { while (input > 0) { if ((input % 10 != 0) && (input % 10 != 1)) { return false; } input = input / 10; } return true; } } |
更改您的代码
从此
2 3 4 5 6 7 | System.out.println(value +" is a not a Binary Number."); break; } else { System.out.println(value +" is a Binary Number."); } |
到这个
2 3 4 5 6 7 8 | System.out.println(value +" is a not a Binary Number."); notBinaryDigit--; } if(binaryDigit >0 { System.out.println(value +" is a Binary Number."); binaryDigit--; break; } |
它会询问值并判断它是否是二进制的,如果是二进制则终止
由于 Scanner 类而出现错误。
您可以运行您的程序并通过像这样删除 Scanner 类来检查您的逻辑。
公共类测试{
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | int value, userValue=34; int binaryDigit = 0, notBinaryDigit = 0; value = userValue; while (userValue > 0) { if ((userValue % 10 == 0) || (userValue % 10 == 1)) { binaryDigit++; } else { notBinaryDigit++; } userValue = userValue / 10; } if (notBinaryDigit > 0) { System.out.println(value +" is a not a Binary Number."); } else { System.out.println(value +" is a Binary Number."); } } |
}
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht