带有Integer和Char的Java菜单

时间:2017-08-30 06:37:02

标签: java

所以我有这段代码:

$q = mysqli_query($connection ,"insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'longitude','30');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'latitude','30');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'tagline','doctor');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'verify_user','on');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'contact_form','off');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'first_name','$doctor_name');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'address','$address');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'phone_number','$mobile');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'nickname','$doctor_name');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'user_type','professional');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'profile_status','active');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'directory_type','127');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'user_profile_specialist','$specialicity');
    insert into wp_abzj_usermeta (user_id,meta_key,meta_value) VALUES ($last_id,'$specialicity','$specialicity')                  
");

所以到现在为止,我应该有一个菜单,它将在用户输入中读取,直到用户键入0.但是,我不希望用户输入0(整数)退出,而是希望代码在用户输入时停止' X' (一个人物)。有没有办法做到这一点?我是Java的新手。

3 个答案:

答案 0 :(得分:0)

您可以使用nextInt()而不是next()。因此,您的选择需要定义为String。在此之后,即使输入1或2之类的数字,这些数字也将作为字符串处理。之后,您可以使用" 1"和#34; x"。

答案 1 :(得分:0)

您应该使用scanner.nextLine().charAt(0)而不是scanner.next().charAt(0),因为接下来不会使用换行符并且可以跳过您的新输入

答案 2 :(得分:0)

似乎你想接受混合输入,包括文本(" x")和整数。

hasNextLine()类有一些实用程序方法可以在使用之前检测扫描程序输入,例如hasNextInt()if (scanner.hasNextInt()) { // It's an integer int choice = scanner.nextInt(); switch (choice) { ... } } else { // A scanner input is always parseable as a string String choice = scanner.nextLine(); if (Objects.equals(choice, "x")) { // Stop, exit, return, terminate and abort } else { // What happens if the user entered an invalid string? // You decide! } } 。使用这些方法,您可以轻松检测是否输入了有效的菜单选项:

Scanner scanner = new Scanner(int);
while (scanner.hasNext()) {
    ...
}

进一步评论

  • 每次迭代,您都会创建一个新的Scanner实例。这是不必要的开销。您最好只创建一个扫描仪,然后重复接受输入:

    <?php 
    
    //username and password of account
    $username = trim($_POST["email"]);
    $password = trim($_POST["password"]);
    echo "username=$username pass:$password";
    $loginUrl = 'https://www.pogrande.com/login.php?action=process';
    //The username or email address of the account.
    define('USERNAME', $username);
    
    //The password of the account.
    define('PASSWORD', $password);
    
    //Set a user agent. This basically tells the server that we are using Chrome ;)
    define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
    
    //Where our cookie information will be stored (needed for authentication).
    define('COOKIE_FILE', 'cookie.txt');
    
    //URL of the login form.
    define('LOGIN_FORM_URL', 'https://www.pogrande.com/login.php');
    
    //Login action URL. Sometimes, this is the same URL as the login form.
    define('LOGIN_ACTION_URL', 'https://www.pogrande.com/login.php?action=process');
    
    
    //An associative array that represents the required form fields.
    //You will need to change the keys / index names to match the name of the form
    //fields.
    $postValues = array(
        'email_address' => USERNAME,
        'password' => PASSWORD
    );
    
    //Initiate cURL.
    $curl = curl_init();
    
    //Set the URL that we want to send our POST request to. In this
    //case, it's the action URL of the login form.
    curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
    
    //Tell cURL that we want to carry out a POST request.
    curl_setopt($curl, CURLOPT_POST, true);
    
    //Set our post fields / date (from the array above).
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
    
    //We don't want any HTTPS errors.
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    //Where our cookie details are saved. This is typically required
    //for authentication, as the session ID is usually saved in the cookie file.
    curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
    
    //Sets the user agent. Some websites will attempt to block bot user agents.
    //Hence the reason I gave it a Chrome user agent.
    curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
    
    //Tells cURL to return the output once the request has been executed.
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    //Allows us to set the referer header. In this particular case, we are 
    //fooling the server into thinking that we were referred by the login form.
    curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
    
    //Do we want to follow any redirects?
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    
    //Execute the login request.
    curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    echo "status=$status"; 
    //Check for errors!
    if(curl_errno($curl)){
        throw new Exception(curl_error($curl));
    }
    
    $url = $_POST['url'];   
    $content = $outp;//json_encode("your data to be sent");
    
    //We should be logged in by now. Let's attempt to access a password protected page
    curl_setopt($curl, CURLOPT_URL, $url);
    
    //Use the same cookie file.
    curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
    
    //Use the same user agent, just in case it is used by the server for session validation.
    curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
    
    //We don't want any HTTPS / SSL errors.
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    
    //Execute the GET request and print out the result.
    curl_exec($curl);//the login is now done and you can continue to get the
    //protected content.
    
    /*
    $url = $_POST['url'];    
    $content = $outp;//json_encode("your data to be sent");
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("Content-type: application/json"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    */
    $json_response = curl_exec($curl);
    
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    
    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }
    
    
    curl_close($curl);
    
    $response = json_decode($json_response, true);
    //alert($response);*/
    }
    }
    
    ?>
    
  • 您应该正确格式化代码。错误的缩进很容易让其他读者感到困惑,包括你自己。