对角线和java程序中的直线绘图

时间:2016-04-14 05:30:20

标签: java input

在下面的程序中你可以看到我允许用户的输入给出一个方向,例如n100,它将向北绘制一条线并将其移动100个空格但是我如何能够允许草图程序做对角线和直线,我明白我能够将输入更改为(0,2)以允许使用像ne这样的对角线,但是当我使用时我的程序不喜欢n,e,s,w等方向。 我该怎么做才能允许这两条线? 这是下面的代码:

enter image description here

<?php
  include 'session.php';
  session_start();
  $db = new mysqli('localhost', 'root', '', 'alumni');
  if(isset($_POST['submit'])):
  extract($_POST);


  $user_check=$_SESSION['login_user'];

  $old_pwd=$_POST['old_password'];
  $pwd=$_POST['password'];
  $c_pwd=$_POST['confirm_pwd'];
  if($old_pwd!="" && $pwd!="" && $c_pwd!="") :


  if($pwd == $c_pwd) :
  if($pwd!=$old_pwd) :
    $sql="SELECT * FROM `alumni` WHERE `username`='$user_check' AND `password` =PASSWORD($old_pwd)";
    $db_check=$db->query($sql);
    $count=mysqli_num_rows($db_check);
  if($count==1) :
    $fetch=$db->query("UPDATE `alumni` SET `password` = PASSWORD($pwd) WHERE `username`='$user_check'");
    $old_pwd=''; $pwd =''; $c_pwd = '';
    $msg_sucess = "Password successfully updated!";
  else:
    $error = "Old password is incorrect. Please try again.";
  endif;
  else :
    $error = "Old password and new password are the same. Please try again.";
  endif;
  else:
    $error = "New password and confirm password do not match.";
  endif;
  else :
    $error = "Please fill all the fields";
  endif;   
  endif;
?> 

2 个答案:

答案 0 :(得分:0)

如果您使用

String direction = input.substring(0, 1);
String distance = input.substring(1);

您只存储和比较字符串的第一个字符,并最终为距离指定无效数字,就好像方向是对角线一样,第二个字符前置于方向。使用String.startsWith()检查给定方向。在if语句中,决定是否在第二个或第三个字符处开始距离。您也可以使用输入作为值进行检查。

...
String distance ;
double distanceAsDouble = 0;

if (input.startsWith("n")) {
    t.heading(0);
    distance = input.substring(1);
} else if (input.startsWith("ne")) {
    t.heading(45);
     distance = input.substring(2);
} else if ...

答案 1 :(得分:0)

String direction = input.replaceFirst("^(\\D*)(.*)$", "$1");
String distance = input.replaceFirst("^(\\D*)(.*)$", "$2").trim();

正则表达式的含义

  • \\D匹配非数字
  • \\d匹配数字
  • postfix * for 0或更多
  • .任何字符
  • ^ begin
  • $ end
  • ()组编号为1
  • $ 1 group 1
相关问题