如何在一个对话框窗口中显示for循环的结果?

时间:2016-09-21 12:39:07

标签: java

我目前正在学习一本关于Java的书。我希望程序的输出只在一个JOptionPane.showMessageDialog窗口中显示几个数字。因此,程序要求用户输入最低和最高的数字,然后显示它们之间的每第7个数字。

 public void count1() {
     int min, max;
     min = Integer.parseInt(JOptionPane.showInputDialog("lowest number"));
     max = Integer.parseInt(JOptionPane.showInputDialog("highest number"));
     for (int i = min; i <= max; i += 7) {
         JOptionPane.showMessageDialog(null, i + " ");   
     } 

 }

我的问题是这个代码块产生了几个显示所有数字的消息窗口,但我想在同一个消息窗口中显示所有这些数字。我在这里搜索过Stackoverflow并看到其他人有同样的问题,但答案一直在暗示“追加”和“stringbuilder”这本书尚未涵盖。当然,必须有一个更简单的方法吗?感谢。

4 个答案:

答案 0 :(得分:1)

你可以做这样的事情

String result = "";
for (int i = min; i <= max; i += 7) {
        result += i + " \n";
     }
JOptionPane.showMessageDialog(null, result);   

答案 1 :(得分:1)

最简单的方法是将所有内容连接成一个String。例如:

<?php
    /* delete.php */
    $img=!empty( $_GET['name'] ) ? $_GET['name'] : false;
    $result=false;


    if( $img ){
        /*

            here you would typically check that the path sent via ajax exists
            and then use unlink to delete the file before sending a response
            to the ajax callback function - the callback would then inform the
            user that the file has been deleted ( or not! )

            For testing though a simple message will suffice so that files are not deleted unnecessarily!!!

            -- uncomment the line below to actually attempt deletion of file.
        */


        if( file_exists( $img ) ){
            #$result = @unlink( $img );
            clearstatcache();
        }


        echo $result ? 'The file '.$img.' was deleted' : 'The file '.$img.' could not be deleted';
    }
?>




<?php
    $root='c:/wwwroot';
?>
<!-- /* admin page that lists images */ -->
<html>
    <head>
        <title>Delete images - no database</title>
        <script>
            function ajax(imagename,callback){
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    if( xhr.status==200 && xhr.readyState==4 ){
                        callback.call( this, xhr.response );
                    }
                };
                xhr.open( 'GET', 'delete.php?name='+imagename, true );
                xhr.send();
            }
            function deleteimage(e){
                e.preventDefault();
                ajax.call( this, e.target.dataset.path+'/'+e.target.dataset.name, cbdeleteimage );
            }
            function cbdeleteimage(r){
                alert( r );
            }
            function bindEvents(){
                var col=document.querySelectorAll('img.delete');
                for( var n in col )if( col[ n ].nodeType==1 ) col[n].addEventListener( 'click', deleteimage, false );
            }

            document.addEventListener( 'DOMContentLoaded', bindEvents, false );
        </script>
    </head>
    <body>
        <?php

            $dir = 'gallery-images/';       /* YOUR path */
            $dir = $root . '/images/misc/'; /* MY test path */

            $files=preg_grep( '@(\.jpg|\.jpeg|\.png|\.bmp|\.gif)@i', glob( $dir . '*.*' ) );
            $html=array();


            foreach( $files as $file ){
                if( $blocal ) $file=str_replace( $root, '', $file );    /* remove MY site root from file names */
                $name = pathinfo( $file, PATHINFO_BASENAME );
                $path = pathinfo( $file, PATHINFO_DIRNAME );

                $html[]="<img class='delete' src='{$file}' data-name='{$name}' data-path='{$path}' />";
            }


            echo implode( PHP_EOL, $html );
        ?>
    </body>
</html>

但是每次使用“+”符号或String.concat(“”)方式时,您将创建一个新对象,并将此对象存储在堆中。因此,如果您想考虑性能,最好使用StringBuilder而不是String,例如:

public void count1() {
     int min, max;
     String elt = new String("");
     min = Integer.parseInt(JOptionPane.showInputDialog("lowest number"));
     max = Integer.parseInt(JOptionPane.showInputDialog("highest number"));
     for (int i = min; i <= max; i += 7) {
         elt = elt + i + " ";
     } 
     JOptionPane.showMessageDialog(null, elt);   
 }

答案 2 :(得分:0)

你应该有一个变量来保存你想要显示的消息,并且它是在循环中组成的。

然后你只需打开对话框。

这是一个简单的解决方案的例子:

public void count1() {
    int min, max;
    min = Integer.parseInt(JOptionPane.showInputDialog("lowest number"));
    max = Integer.parseInt(JOptionPane.showInputDialog("highest number"));
    StringBuilder buff = new StringBuilder();
    for (int i = min; i <= max; i += 7) {
        buff.append(i).append(" ");
    }
    JOptionPane.showMessageDialog(null, buff.toString());
}

以下是正确解决方案的示例:

DesiredCapabilities capabilities = new DesiredCapabilities();  
  capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"Android");
  capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");  
  capabilities.setCapability(MobileCapabilityType.VERSION, "4.4.2");  
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "com.whatsapp");  
  capabilities.setCapability("appActivity", "com.whatsapp.Main");  
  driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);  
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  
  driver.quit();

答案 3 :(得分:0)

将您存储在 arrayList 中,然后您可以在提醒对话中显示该列表。